我有一个有多个主要类的maven项目。我想从这些项目中生成可运行的Jars(包括所有依赖项)。我目前有以下构建配置(使用maven.assembly):
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>classpath.to.my.mainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
他们是否可以通过maven-assembly实现这一目标?如果没有,实现目标的最简单方法是什么?
答案 0 :(得分:23)
你可以做到。您需要为正在构建的每个工件单独执行(即,为每个工件提供自己的ID,但您可以将阶段保留为默认值),并且您需要指定finalName和archive / manifest / mainClass 为每个。
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>build-a</id>
<configuration>
<archive>
<manifest>
<mainClass>foobar.Aclass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>foobar_a.jar</finalName>
</configuration>
</execution>
<execution>
<id>build-b</id>
<configuration>
<archive>
<manifest>
<mainClass>foobar.Bclass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>foobar_b.jar</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
答案 1 :(得分:22)
我无法用令人满意的方式解决maven-assembly-plugin
这个问题,所以我选择了另一种解决方案。我使用了onejar-maven-plugin:
<build>
<plugins>
<plugin>
<groupId>org.dstovall</groupId>
<artifactId>onejar-maven-plugin</artifactId>
<version>1.4.4</version>
<executions>
<execution>
<id>build-first</id>
<configuration>
<mainClass>classpath.to.first.Main</mainClass>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
<filename>first-runnable.jar</filename>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
<execution>
<id>build-second</id>
<configuration>
<mainClass>classpath.to.second.Main</mainClass>
<attachToBuild>true</attachToBuild>
<classifier>onejar</classifier>
<filename>second-runnable.jar</filename>
</configuration>
<goals>
<goal>one-jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>onejar-maven-plugin.googlecode.com</id>
<url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
</pluginRepository>
</pluginRepositories>
答案 2 :(得分:9)
The top answer工作如果你没有使用捆绑到jar文件中的任何配置(或任何资源)(例如,Spring Framework自动绑定的配置) 。
幸运的是,此解决方案也可以与maven-shade-plugin
一起使用,并且您在onejar-maven-plugin
上没有上述问题。
此外,maven-shade-plugin
正在积极维护,而不是onejar-maven-plugin
,这是炼狱中的googlecode。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>build-first</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>classpath.to.first.Main</mainClass>
</transformer>
</transformers>
<finalName>first-runnable</finalName>
</configuration>
</execution>
<execution>
<id>build-second</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>classpath.to.second.Main</mainClass>
</transformer>
</transformers>
<finalName>second-runnable</finalName>
</configuration>
</execution>
</executions>
</plugin>
答案 3 :(得分:2)
要指定一个对我非常有用的先前答案, 你需要添加阶段包和目标程序集并运行mvn run clean package,pom如下:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>build-a</id>
<configuration>
<archive>
<manifest>
<mainClass>firstMainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>a.jar</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
<execution>
<id>build-b</id>
<configuration>
<archive>
<manifest>
<mainClass>SecondMainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>b.jar</finalName>
</configuration>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>