使用 maven-assembly-plugin 创建两个可执行的 jar [英] Creating Two Executable Jars Using maven-assembly-plugin
问题描述
我有一个 Maven 项目,我想从中创建两个可执行的 jar 文件.一个将由用户交互使用,第二个将作为读取前者生成的日志文件的计划作业运行.最后,除了 MANIFEST.MF 文件中的 Main-Class 属性外,我希望这两个 jar 文件是相同的.
I have a Maven project and I want to create two executable jar files from it. One will be used interactively by users and a second will be run as a scheduled job that reads the log files produced by the former. In the end, I would expect the two jar files to be identical except for the Main-Class attribute in the MANIFEST.MF file.
我正在使用 maven-antrun-plugin 创建一个可执行 jar,这似乎工作正常,直到我尝试通过引入 Maven 配置文件来创建第二个 jar 文件.我的 POM 文件的相关部分如下所示:
I am using maven-antrun-plugin to create an executable jar and this seemed to be working fine until I tried to create a second jar file by introducing Maven profiles. The relevant section of my POM file looks like this:
<profiles>
<profile>
<id>publisher</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}</finalName>
<archive>
<manifest>
<mainClass>fully.qualified.path.Publisher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>logReader</id>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
...
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>${project.artifactId}-logReader</finalName>
<archive>
<manifest>
<mainClass>fully.qualified.path.LogReader</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
实际上,两者之间的唯一区别是插件中定义的finalName"和mainClass"元素.
Really, the only difference between the two is the "finalName" and "mainClass" elements as defined within the plugin.
当我尝试在两个配置文件上执行 mvn:package 时(顺便说一下,我使用的是 IntelliJ IDEA),我得到两个 .jar 文件,但一个是正确的,另一个是不正确的.正确"的包含所有依赖项和有效的 MANIFEST.MF 文件.不正确"的不包含任何依赖项,并且 MANIFEST.MF 文件缺少我需要的Main-Class"属性,以使其可执行.
When I try to execute mvn:package on both profiles (I'm using IntelliJ IDEA, by the way), I get two .jar files, but one is correct and the other is incorrect. The "correct" one contains all the dependencies and a valid MANIFEST.MF file. The "incorrect" one contains no dependencies and the MANIFEST.MF file lacks the "Main-Class" property that I need in order for it to be executable.
我发现,如果我只运行一个或另一个配置文件,它运行良好,但是,如果我尝试同时执行两个配置文件,它就会失败.我的日志中也有以下注释,但我必须承认我并不完全理解他们在说什么:
I have found that if I ever run just one profile or the other, that it works fine but, if I try to execute both profiles at once, it fails. I also get the following notes in my log, but I must admit that I don't completely understand what they are saying:
[INFO] Building jar: .../target/publisher.jar
...
[INFO] Building jar: .../target/publisher-logReader.jar
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: .../target/publisher-logReader.jar, it will become the file for main project artifact.
NOTE: If multiple descriptors or descriptor-formats are provided for this project, the value of this file will be non-deterministic!
[WARNING] Replacing pre-existing project main-artifact file: .../target/publisher.jar with assembly file: .../target/publisher-logReader.jar
对此有什么想法吗?是否有可能以这种方式使用单个 mvn:package 生成两个 jar 文件,或者我在吠错树?
Any thoughts about this? Is it possible to produce two jar files with a single mvn:package in this way, or am I barking up the wrong tree?
谢谢!
推荐答案
所以当我发布这个的时候,我发现了这个线程:
So as soon as I posted this, I found this thread:
创建来自单个 Maven 项目的多个可运行的 jar(包括依赖项)
这使用了不同的方法,因为它不使用两个配置文件,而是使用两次执行,例如:
This uses a different approach in that it doesn't use two profiles, it uses two executions, as such:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>build-publisher</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>fully.qualified.path.Publisher</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
<execution>
<id>build-logReader</id>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>fully.qualified.path.LogReader</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-logReader</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
这似乎奏效了.这个故事的寓意似乎是我不完全了解配置文件或何时应该使用它们.
This seems to be working. The moral of the story seems to be that I don't completely understand profiles or when they should be used.
这篇关于使用 maven-assembly-plugin 创建两个可执行的 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!