使用 Maven 创建独立的源版本

发布于 2024-07-27 16:31:48 字数 600 浏览 4 评论 0原文

到目前为止,我们公司都在使用 Ant。 每当我们想要将应用程序发送到客户端时,我们都会运行一个特殊的 Ant 脚本,该脚本将所有源代码与所有 jar 库和 Ant 本身以及一个简单的批处理文件打包在一起。

然后,客户端可以将文件放在根本没有网络访问权限(甚至没有 Ant)的计算机上并运行批处理文件。 只要计算机具有有效的 JDK,批处理脚本就会使用 jar 编译所有代码并创建最终由客户端部署在应用程序服务器上的 WAR/EAR。

最近我们迁移到 Maven 2。但我还没有找到一种方法来做同样的事情。 我见过 Maven 程序集插件,但这只是创建源代码发行版或二进制发行版。 我们的场景实际上是一种混合,因为它包含我们的源代码,但包含我们使用的库的二进制 jar(例如 Spring、Hibernate)

那么是否可以使用 Maven 创建一个可以在计算机上运行的独立程序集/发布/包根本无法访问网络??? 这意味着所有库都应该包含在里面。

如果 Maven 本身也包含在里面,那就额外的好处,但这不是一个严格的要求。 最终的包应该可以通过一个命令轻松编译(系统管理员可以轻松执行)。

我正在考虑为此编写自己的 Maven 插件,但我怀疑有人已经遇到过这种情况。

Up until now we used Ant in my company. Whenever we wanted to send the application to the client we run a special Ant script that packaged all our source code with all jar libraries and Ant itself along with a simple batch file.

Then the client could put the files on a computer with no network access at all (and not even Ant) and run the batch file. As long as the computer had a valid JDK the batch script would compile all the code using the jars and create a WAR/EAR that would finally be deployed by the client on the application server.

Lately we migrated to Maven 2. But I haven't found a way to do the same thing. I have seen the Maven assembly plugin but this just creates source distributions or binary ones. Our scenario is actually a mix since it contains our source code but binary jars of the libraries we use (e.g. Spring, Hibernate)

So is it possible to create with Maven a self-contained assembly/release/package that one can run in a computer with no network access at all??? That means that all libraries should be contained inside.

Extra bonus if Maven itself is contained inside as well, but this is not a strict requirement. The final package should be easily compiled by just one command (easy for a system administrator to perform).

I was thinking of writing my own Maven plugin for this but I suspect that somebody has already encountered this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天赋异禀 2024-08-03 16:31:48

在您的开发环境中,如果您在构建插件下包含以下内容

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

并调用 mvn assembly: assembly,您将在目标文件夹中获得 yourApp-version-with-dependency.jar 。 这是一个自给自足的 jar,并且具有主类 MANIFEST.MF 条目,任何人都可以双击并运行该应用程序。

From your dev environment, if you include the following under build plugins

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

and invoke mvn assembly:assembly, you would get yourApp-version-with-dependencies.jar in the target folder. This is a self-sufficient jar, and with a Main-class MANIFEST.MF entry, anybody can double click and run the application.

清晨说晚安 2024-08-03 16:31:48

您可以尝试这种方法:

  • 使用 mvn ant:ant 创建 ant 构建
    来自 maven 项目的脚本
  • 确保 ant 是项目依赖项
  • 使用程序集构建 ant
    系统

或计划 b:

  • 使用 mvn ant:ant 创建 ant 构建
    来自 maven 项目的脚本
  • 确保 ant 是项目依赖项
  • 编写一个“引导类”来调用 Ant 并运行构建
  • 使用 appassembler 构建一个
    脚本化构建和安装环境

在 b 计划中,您需要编写脚本来从打包的源 jar 中的某个位置设置源树,然后使用 appassembler 构建 bat 或 sh 脚本来调用引导程序并通过 ant 进行构建。 您的引导程序可以在构建之前或之后执行您需要执行的任何操作。

希望这可以帮助。

You might try this approach:

  • Use mvn ant:ant to create ant build
    scripts from a maven project
  • Make sure ant is a project dependency
  • Use the assembly to build an ant
    system

or plan b:

  • Use mvn ant:ant to create ant build
    scripts from a maven project
  • Make sure ant is a project dependency
  • Write a "bootstrap class" to call Ant and run the build
  • Use appassembler to build a
    scripted build and install environment

In plan b, you'd write scripts to set up a source tree someplace from the packaged source jars, and then use the appassembler build bat or sh scripts to call the bootstrap and build via ant. Your bootstrap can do anything you need to do before or after the build.

Hope this helps.

傲娇萝莉攻 2024-08-03 16:31:48

也许我针对类似问题提交的答案可能会有所帮助。 请参阅maven 可以收集项目的所有依赖 jar 来帮助应用程序部署吗? 缺少的一点是如何将源代码包含在程序集中。 我必须想象有某种方法可以使用程序集插件来管理它。 这也没有解决在发行版中包含 Maven 的问题。

从 Ant 迁移到 Maven 的原因是什么? 听起来您使用 Ant 解决方案一切都进展顺利,那么 Maven 在这里给您带来了什么?

如果只是依赖管理,有 从 Ant 利用 Maven 的技术 可以让您两全其美世界。

Perhaps an answer that I submitted for a similar question could be of some assistance. See Can maven collect all the dependant jars for a project to help with application deployment? The one piece missing is how to include the source code in the assembly. I have to imagine that there is some way to manage that with the assembly plugin. This also doesn't address the inclusion of Maven in the distribution.

What was the reason for moving from Ant to Maven? It sounds like you had everything worked out well with the Ant solution, so what is Maven buying you here?

If it is just dependency management, there are techniques for leveraging Maven from Ant that give you the best of both worlds.

一身仙ぐ女味 2024-08-03 16:31:48

源插件将为您提供一个包含项目“source:jar”源的 jar。 然后,您可以使用程序集插件将内部项目中的源 jar(使用源引用这些源 jar)和外部项目中的二进制 jar 合并到一个发行版中。

但是,至于将其变成可编译单元,我没有任何建议。 你当然可以捆绑maven,但你需要创建一个包含构建项目所需的所有插件的捆绑包! 我不知道有任何现有工具可以做到这一点。

the source plugin will give you a jar containing the source of a probject "source:jar". you could then use the assembly plugin to combine the source jars from your internal projects (using the sources to reference these source jars) and the binary jars from the external projects into one distribution.

however, as for turning this into a compilable unit, i have no suggestions. you could certainly bundle maven, but you'd need to create a bundle containing all the plugins you need to build your project! i don't know of any existing tool to do that.

悲念泪 2024-08-03 16:31:48

这就是我这样做的...在 pom 的构建部分添加以下内容:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后在配置文件部分添加此位:

<profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

当我执行 Maven 安装时,它会构建 jar 并检查来源。

This is how I do it... on the build part of the pom add in this:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-source-plugin</artifactId>
            <executions>
                <execution>
                    <id>attach-sources</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

And then on the profiles section add this bit in:

<profiles>
        <profile>
            <id>release</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

And when I do a maven install it builds the jar and also checks in a jar of the source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文