只在Maven中创建可执行的jar-with-dependencies [英] Only create executable jar-with-dependencies in Maven

查看:1251
本文介绍了只在Maven中创建可执行的jar-with-dependencies的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 pom.xml 中,我使用maven-assembly-plugin在运行mvn clean install时创建一个可执行的jar-with-dependencies。
现在它首先创建不可执行的name-version.jar,然后创建name-version-jar-with-dependencies.jar。

In my pom.xml I use the maven-assembly-plugin to create an executable jar-with-dependencies when running "mvn clean install". Now it first creates the non-executable "name-version.jar" and after that the "name-version-jar-with-dependencies.jar".

我能否以某种方式配置 pom.xml ,以便它不会创建不可执行的JAR文件?

Can I configure the pom.xml somehow, so that it doesn't create the non-executable JAR file?

目前我使用< appendAssemblyId> false< / appendAssemblyId>所以它只是覆盖了第一个文件...

At the moment I use <appendAssemblyId>false</appendAssemblyId> so it just overwrites the first file...

此外,我还得到了几个...已添加,跳过消息。我可以以某种方式阻止它们吗?

Also I get several "... already added, skipping" messages. Can I somehow prevent them?

这是我的 pom.xml中的maven-assembly-plugin定义

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>2.2-beta-5</version>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <archive>
            <manifest>
                <mainClass>my.main.class</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>


推荐答案

我想我已经弄清楚如何做到这一点。你必须离开jar,否则你将失去编译器和资源复制,以及建立你需要的jar所带来的一切。

I think I've figured out how to do this. You have to leave the jar, otherwise you'll lose the compiler and resource copying, and everything else that goes along with building a jar that you need.

我的解决方案涉及基本上是欺骗maven,所以我想要提醒的是,在未来的版本中这很可能无法正常工作......

My solution involves essentially tricking maven, so I guess the thing to beware of is that it's very possible that in future releases this won't work...

<build>
    <defaultGoal>install</defaultGoal>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <finalName>dumpster-diver</finalName>
                <appendAssemblyId>false</appendAssemblyId>
                <archive>
                    <manifest>
                        <mainClass>DumpsterDiver</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>

请注意,在列出的构建配置中,maven-assembly-plugin和build的finalName本身就配。另请注意< appendAssemblyId> false< / appendAssemblyId> 。如果这样做,构建和程序集jar的输出文件名将完全匹配。 Maven肯定会警告你,但是会把你想要的jar放在你的目标目录和你的本地存储库中。

Note that in the build configuration listed, the finalName of both the maven-assembly-plugin AND the build itself match. Also note the <appendAssemblyId>false</appendAssemblyId>. If you do this, the output filename of the build and of the assembly jar will match exactly. Maven will definitely warn you about this, but will put the jar you want in your target directory AND in your local repository.

这是'mvn clean install'的输出在这个项目上,以及目标目录和本地存储库中的jar上的jar -tvf:

Here is the output of 'mvn clean install' on this project, as well as jar -tvf on both the jar in the target directory and in the local repository:

$ mvn clean install
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building dumpster-diver
[INFO]    task-segment: [clean, install]
[INFO] ------------------------------------------------------------------------
[INFO] [clean:clean {execution: default-clean}]
[INFO] Deleting directory <currentDirectory>/target
[INFO] [resources:resources {execution: default-resources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] [compiler:compile {execution: default-compile}]
[INFO] Compiling 1 source file to <currentDirectory>/target/classes
[INFO] [resources:testResources {execution: default-testResources}]
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory <currentDirectory>/src/test/resources
[INFO] [compiler:testCompile {execution: default-testCompile}]
[INFO] No sources to compile
[INFO] [surefire:test {execution: default-test}]
[INFO] No tests to run.
[INFO] [jar:jar {execution: default-jar}]
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar
[INFO] [assembly:single {execution: default}]
[INFO] Processing DependencySet (output=)
[WARNING] Artifact: <groupId>:dumpster-diver:jar:0.1.0-SNAPSHOT references the same file as the assembly destination file. Moving it to a temporary location for inclusion.
[INFO] Building jar: <currentDirectory>/target/dumpster-diver.jar
[WARNING] Configuration options: 'appendAssemblyId' is set to false, and 'classifier' is missing.
Instead of attaching the assembly file: <currentDirectory>/target/dumpster-diver.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: <currentDirectory>/target/archive-tmp/dumpster-diver.jar
with assembly file: <currentDirectory>/target/dumpster-diver.jar
[INFO] [install:install {execution: default-install}]
[INFO] Installing <currentDirectory>/target/dumpster-diver.jar to /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4 seconds
[INFO] Finished at: Thu Aug 18 13:08:56 EDT 2011
[INFO] Final Memory: 22M/618M
[INFO] ------------------------------------------------------------------------
krohr@krohr-Latitude-E6500:<currentDirectory>
$ jar -tvf ./target/dumpster-diver.jar | grep -i "dump"
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
     0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/
  2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml
       126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties
krohr@krohr-Latitude-E6500:<currentDirectory>
$ jar -tvf /opt/m2repo/<groupId>/dumpster-diver/0.1.0-SNAPSHOT/dumpster-diver-0.1.0-SNAPSHOT.jar | grep -i "dump"
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
  6831 Thu Aug 18 13:08:54 EDT 2011 DumpsterDiver.class
     0 Thu Aug 18 13:08:56 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/
  2580 Thu Aug 18 13:01:46 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.xml
   126 Thu Aug 18 13:08:54 EDT 2011 META-INF/maven/<groupId>/dumpster-diver/pom.properties

这篇关于只在Maven中创建可执行的jar-with-dependencies的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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