使用maven-assembly-plugin创建两个可执行的jar [英] Creating Two Executable Jars Using maven-assembly-plugin

查看:174
本文介绍了使用maven-assembly-plugin创建两个可执行的jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Maven项目,我想从中创建两个可执行的jar文件。一个将由用户以交互方式使用,另一个将作为预定作业运行,该作业读取前者生成的日志文件。最后,我希望两个jar文件相同,除了MANIFEST.MF文件中的Main-Class属性。

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-插件创建一个可执行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:

创建多个可运行的Jars(具有相关性)包含)来自单个Maven项目

这使用了一种不同的方法,因为它不使用两个配置文件,它使用两个执行,如下: / p>

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆