在构建生命周期之外分发/使用 Maven 项目的推荐实践

发布于 2024-12-01 19:03:40 字数 635 浏览 1 评论 0原文

在很多情况下,我不确定处理 Maven 项目的依赖关系的最佳方法是什么。那是在执行一个 jar 时,这就是 mvn package 的结果。

我尝试过的事情

1) Maven-shade 插件 对于某些用例(例如 hadoop 作业),我发现使用 Maven 阴影插件与打包步骤集成很方便(它构建了一个全包的 uber-jar )。缺点是超级罐子太大了。另外,我无法让 Maven 阴影处理 datanucleus 依赖项,因为它弄乱了一些东西。

2) 将依赖项与 jar 一起分发。

//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass

生成一个包含所有依赖项以及 jar 的目录。

我想做的是

3)能够分发 jar 并在执行 jar 时处理依赖项。由于 mvn package 将 pom 放在 jars 清单文件夹中,所以所有信息都在那里,对吗? 。现在,我希望有一个单行命令能够运行这个 jar,要求 Maven 管理依赖项。如果有人知道这样的东西是否可以用作 hadoop 作业,那就更棒了。

There are many occasions where I am not sure whats the best to handle dependencies for a maven project. That is while executing a jar thats the result of mvn package.

Things I have tried

1) Maven-shade plugin
For some use cases ( such as hadoop jobs ) I find it convenient to use the Maven shade plugin to integrate with the package step ( it builds an all inclusive uber-jar ). The downside is that the uber-jar is too massive. Also, I cant get the maven shade to work on datanucleus dependencies as it messes up something.

2) distribute the dependencies along with the jar.

//something like this
$ mvn package dependency:copy-dependencies
$ java -cp target/project.jar:target/dependency com.MyMainClass

generates a directory with all the dependencies along with the jar.

What I would like to do is

3) just be able to distribute the jar and handle the dependencies while executing the jar. Since mvn package puts the pom in the jars manifest folder, all the information is there right ? . Now, I would like it if there was a one line command to be able to run this jar asking maven to manage the dependencies. Even more awesome if someone knows if such a thing can be used as a hadoop job.

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

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

发布评论

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

评论(1

長街聽風 2024-12-08 19:03:40

我认为您的第 3 点中的问题没有解决方案。

我的三个选项,在所有情况下,您都可以使用简单的 java -jar target/project.jar 运行应用程序。不需要更多了。

1. 带有 jar-with-dependency 描述符的 Maven 程序集插件

(此处缺少 jar 配置中的清单)

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

2. Maven 依赖项插件

Maven 依赖项插件、正确配置的 jar(类路径)和用于将所有内容打包在一起的 Maven 程序集自定义描述符。

(如果您的项目不使用快照依赖项,您可以简化这一点)

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>your.package.MainApp</mainClass>
                <packageName>your.package</packageName>
                <classpathLayoutType>custom</classpathLayoutType>
                <customClasspathLayout>lib/${artifact.artifactId}.${artifact.extension}/customClasspathLayout>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>true</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeScope>test</excludeScope>
                <includeScope>compile</includeScope>
                <stripVersion>true</stripVersion>
            </configuration>
        </execution>
    </executions>
</plugin>

3. Maven One Jar

<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
        <execution>
            <configuration>
                <attachToBuild>true</attachToBuild>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

I think there is no solution for question in your point 3.

My three options, in all cases you can run application with simple java -jar target/project.jar. Nothing more is needed.

1. Maven Assembly Plugin with jar-with-dependencies descriptor

(manifest in jar configuration is missed here)

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

2. Maven dependency plugin

Maven dependency plugin, properly jar configured (classpath) and maven assembly custom descriptor for packing all together.

(you can simplify this, if your project is not using snapshot dependencies)

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>true</addClasspath>
                <mainClass>your.package.MainApp</mainClass>
                <packageName>your.package</packageName>
                <classpathLayoutType>custom</classpathLayoutType>
                <customClasspathLayout>lib/${artifact.artifactId}.${artifact.extension}/customClasspathLayout>
            </manifest>
        </archive>
    </configuration>
</plugin>
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/lib</outputDirectory>
                <overWriteReleases>true</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
                <excludeScope>test</excludeScope>
                <includeScope>compile</includeScope>
                <stripVersion>true</stripVersion>
            </configuration>
        </execution>
    </executions>
</plugin>

3. Maven One Jar

<plugin>
    <groupId>org.dstovall</groupId>
    <artifactId>onejar-maven-plugin</artifactId>
    <version>1.4.4</version>
    <executions>
        <execution>
            <configuration>
                <attachToBuild>true</attachToBuild>
            </configuration>
            <goals>
                <goal>one-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文