【发布时间】:2016-08-21 06:54:20
【问题描述】:
我是 maven 新手,您能否帮我提供一个示例 pom 文件,说明如何为我的 spring 项目创建属于特定包的 jar 文件。
下面是我的两个包
com.net.foo
com.org.bar
我想单独为 com.org.bar 包创建 jar 文件,名称为 org.jar
您能帮忙说明一下如何实现吗?
谢谢
【问题讨论】:
我是 maven 新手,您能否帮我提供一个示例 pom 文件,说明如何为我的 spring 项目创建属于特定包的 jar 文件。
下面是我的两个包
com.net.foo
com.org.bar
我想单独为 com.org.bar 包创建 jar 文件,名称为 org.jar
您能帮忙说明一下如何实现吗?
谢谢
【问题讨论】:
由于您是 maven 新手,我应该告诉您每个 maven 模块(每个 pom.xml)都应该生成一个 jar。从单个 pom.xml 生成两个 jar 是一种反模式。您可能想要创建一个多模块 maven。
在这种情况下,您使用
创建父 pom.xml<packaging>pom</packaging>
并让它包含两个 maven 项目
<modules>
<module>foo</module>
<module>bar</module>
</modules>
现在您可以通过一次构建来构建两个 jar,但每个 jar 都是自己的 maven 项目,这是正确的模式。
话虽如此,如果你真的想按包分开,我想你可以使用这个:
https://maven.apache.org/plugins/maven-jar-plugin/examples/include-exclude.html
但是你不能简单地重复两次 maven-jar-plugin。您必须创建 2 个配置文件或 2 个执行来强制 maven 运行 jar 插件两次。最简单的是创建 2 个执行。请参阅下面的代码。
现在,关于如何命名你生成的 jar,你需要在 maven-jar-plugin 上设置 finalName 配置
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<finalName>myJar</finalName>
</configuration>
</plugin>
把它们放在一起:
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>foo</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<finalName>foo</finalName>
<includes>
<include>**/foo/*</include>
</includes>
</configuration>
</execution>
<execution>
<id>bar</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<finalName>bar</finalName>
<includes>
<include>**/bar/*</include>
</includes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
请注意,如果您遵循此方法,生成的 jar 文件不会自动被视为项目工件,因此您需要做额外的工作才能将它们安装/部署为 maven 工件。我强烈建议您使用多模块方法。
【讨论】:
您需要使用 maven-jar-plugin。
示例用法:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>default-jar</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<excludes>
<exclude>**/bar/**</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
【讨论】: