java Maven:程序集插件根本没有运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10929199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven: assembly-plugin is not run at all
提问by posdef
I am pretty new to Maven, tried to learn a couple of times before and figured I was better off without Maven. Now, (un)fortunately I have to use Maven in a project I'd like to contribute to. My problem at this point is regarding the packaging. I'd like to produce a self-contained (aka "fat") jar with all dependencies included, and a colleague who's played around with Maven helped me out with the pom file.
我对 Maven 很陌生,之前尝试学习了几次,并认为没有 Maven 会更好。现在,(不)幸运的是,我必须在我想参与的项目中使用 Maven。我现在的问题是关于包装。我想制作一个包含所有依赖项的独立(又名“胖”)jar,一位玩过 Maven 的同事帮助我解决了 pom 文件。
I have checked his pom files, as well as samples here on SO. The xml looks legit but the plugin is not run at all. Note that I get no errors, everything goes fine except I get no "fatjar". Any ideas what might be the cause of this problem?
我已经检查了他的 pom 文件,以及 SO 上的示例。xml 看起来合法,但插件根本没有运行。请注意,我没有收到任何错误,一切都很好,只是我没有收到“fatjar”。任何想法可能是导致此问题的原因?
Below is the relevant portion of the pom.xml. I have seen contradicting code samples with regards to positioning of <configuration>
and <executions>
tags, tried pretty much every variation I have found, still no joy.
下面是 pom.xml 的相关部分。我看到过关于定位<configuration>
和<executions>
标签的相互矛盾的代码示例,几乎尝试了我发现的所有变体,但仍然没有乐趣。
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>make-jar-with-dependencies</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>ProjectName</finalName>
<appendAssemblyId>true</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>org.mydomain.ProjectName</mainClass>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
Edit_1the console output from mvn clean package
as asked by @khmarbaise
按@khmarbaise的mvn clean package
要求编辑_1控制台输出
[INFO]
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ myproject ---
[INFO] Deleting /home/user/workspace/project/target
[INFO]
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ myproject ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 41 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ myproject ---
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 292 source files to /home/user/workspace/project/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ myproject ---
[debug] execute contextualize
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/user/workspace/project/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:2.3.2:testCompile (default-testCompile) @ myproject ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ myproject ---
[INFO] No tests to run.
[INFO] Surefire report directory: /home/user/workspace/project/target/surefire-reports
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Results :
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.3.2:jar (default-jar) @ myproject ---
[INFO] Building jar: /home/user/workspace/project/target/myproject-2.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.210s
[INFO] Finished at: Thu Jun 07 11:45:14 CEST 2012
[INFO] Final Memory: 18M/184M
[INFO] ------------------------------------------------------------------------
回答by khmarbaise
I would recommend to create a uberjar by using the maven-shade-plugin, cause it's intention is exactly that purpose. You can do that with the maven-assembly-plugin as well.
我建议使用maven-shade-plugin创建一个 uberjar ,因为它的意图正是这个目的。您也可以使用 maven-assembly-plugin 来做到这一点。
So after taking a look into your pom i understand the problem. First you defined the maven-assembly-plugin in the pluginManagement block which will NOTexecute a plugin furthermore you have defined the maven-assembly-plugin as a dependency separately which is superfluous. Which means simply remove the following from your pom:
因此,在查看您的 pom 后,我明白了问题所在。首先,您在 pluginManagement 块中定义了 maven-assembly-plugin,该块不会执行插件,此外您已将 maven-assembly-plugin 定义为单独的依赖项,这是多余的。这意味着只需从您的 pom 中删除以下内容:
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<type>maven-plugin</type>
</dependency>
You should define the maven-assembler-plugin like this:
您应该像这样定义 maven-assembler-plugin:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
...all your configuration here..
</plugin>
</plugins>
</build>
Furthermore i've seen many repository definition which should be handled by repository manager instead. About the repositories in your pom i can recommend reading the sonatype informationfurthermore you should think about some else will use the project behind a proxy etc. than he has to change you pom to get i working or can't use it cause you defined repositories in your pom he can't reach.
此外,我已经看到许多存储库定义应该由存储库管理器处理。关于您的 pom 中的存储库,我建议您进一步阅读sonatype 信息,您应该考虑其他一些将使用代理等背后的项目,而不是他必须更改您的 pom 才能让我工作或无法使用它,因为您定义了存储库在你的pom中,他无法到达。
回答by Neil
I want to offer one more suggestion of an error that I had: My xml looked like this:
我想再提供一个关于我遇到的错误的建议:我的 xml 看起来像这样:
<plugins>
<plugin>
<groupId>org.maven...</groupId>
<artifactId>myArtifact</artifact>
<configuration>
<descriptorRefs>....</descriptorRefs>
...
<executions>
<execution>
<phase>package</phase>
....
</execution>
</executions>
</configuration>
</plugin>
</plugins>
Whereas the <executions>
block should be a sibling of the <configuration>
block, not a child. Soon as I fixed that, my plugin started executing within my build again.
而<executions>
块应该是块的兄弟<configuration>
,而不是孩子。一旦我解决了这个问题,我的插件又开始在我的构建中执行。
回答by David Hladky
I had one other view on this problem, that helped me to resolve the %subj%. If you dig the articles about how to make executable jar and jar with dependencies, they usually provide code snippets, that a newbie tries to copy paste to their pom.xml. Those, that do not know much about the actual structure of pom.xml file (aka me) it is quite difficult task. I ended with this (not working) solution:
我对这个问题有另一种看法,它帮助我解决了 %subj%。如果您挖掘有关如何制作具有依赖项的可执行 jar 和 jar 的文章,他们通常会提供代码片段,新手尝试将其复制粘贴到他们的 pom.xml 中。那些对 pom.xml 文件(又名我)的实际结构不太了解的人,这是一项非常艰巨的任务。我以这个(不工作)解决方案结束:
<project>
......
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
............
</configuration>
<executions>
..............
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
If you know what pluginManagementdoes, you would not do the same mistake. But for me it was a big unknown and if you never used parent projects, make sure you do not fall into the same trap. With some adjustments here is a working maven-assembly-plugin setup (with the right context in pom.xml)
如果您知道pluginManagement 的作用,您就不会犯同样的错误。但对我来说,这是一个很大的未知数,如果您从未使用过父项目,请确保您不会落入同一个陷阱。这里有一些调整是一个有效的 maven-assembly-plugin 设置(在 pom.xml 中有正确的上下文)
<?xml version="1.0" encoding="UTF-8"?>
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.blah.blah</groupId>
<artifactId>some-artifact</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId> <!-- nicer looking jar name -->
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.blah.blah.YourMainClass</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>
</plugins>
</build>
</project>
回答by MTranchant
Maybe you can use the shadeplugin instead (that's the one I use).
也许您可以改用shade插件(这是我使用的插件)。
Note the dependencies are included in the final JAR.
请注意,依赖项包含在最终 JAR 中。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
<configuration>
<finalName>standalone-${artifactId}</finalName>
<archive>
<manifest>
<mainClass>com.mypckg.Launcher</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Regards
问候
回答by Junchen Liu
the dependency looks like
依赖看起来像
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
also you need to define a assembly.xml , here is a example
你还需要定义一个 assembly.xml ,这是一个例子
<assembly>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
</dependencySet>
</dependencySets>
<fileSets>
<!-- move all the .jar from target to /lib -->
<fileSet>
<directory>target</directory>
<outputDirectory>/lib</outputDirectory>
<includes>
<include>*.jar</include>
</includes>
<!-- include *.jar places the same version of >App.jar
twice in the lib, we need remove one -->
<excludes>
<exclude>App*.jar</exclude>
</excludes>
</fileSet>
<!-- move .bat from scripts to /scripts -->
<fileSet>
<directory>src/main/assembly/scripts</directory>
<outputDirectory>/bin</outputDirectory>
<includes>
<include>*.bat</include>
</includes>
</fileSet>
</fileSets>
</assembly>