可以说我有3个软件包,并且需要为每个仅包含当前软件包内容的软件包创建一个jar。我的尝试是:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>first-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>first-jar</classifier>
<excludes>
<exclude>/maven.task.3/src/main/java/third/ThirdMain.java
</exclude>
<exclude>/maven.task.3/src/main/java/second/SecondMain.java
</exclude>
</excludes>
</configuration>
</execution>
<execution>
<id>second-jar</id>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>second-jar</classifier>
<excludes>
<exclude>/maven.task.3/src/main/java/first/FirstMain.java
</exclude>
<exclude>/maven.task.3/src/main/java/third/ThirdMain.java
</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
这确实会创建不同的jar,但是其中的内容是相同的,这意味着exclude子句不起作用。我尝试仅排除类(相对/绝对路径)和仅包。不要问为什么我要这样做是为了做作业,这没有什么意义!这是我尝试执行的方式,如果还有其他更有效的方式,请随时与我分享!
编辑:不得使用模块化结构,它必须是一个单独的项目。
提前致谢
评论中已经回答了这个问题,但这是一个示例。
父pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.essexboy</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>jar1</module>
<module>jar2</module>
</modules>
</project>
还有2个子/模块poms,只有artifactId不同:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>parent</artifactId>
<groupId>com.essexboy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>jar1</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
</build>
</project>
额外信息
我使用以下命令创建了父项:
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE
然后使用命令创建2个模块
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE
如果您必须有一个jar项目(没有模块和父项目),则可以使用shade-plugin进行操作:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.essexboy</groupId>
<artifactId>double-jar</artifactId>
<version>1.0-SNAPSHOT</version>
<name>double-jar</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>1</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jar1</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/essexboy/App2*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
<execution>
<id>2</id>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>jar2</finalName>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>com/essexboy/App1*</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
@Esses Boy是的,正如我上面提到的,这是一种更干净,更轻松的方法,但是我必须仅使用一个项目而不是模块化结构来执行此操作。
@AlexVulchev我已经更新了答案,显示了如何使用shade插件执行此操作,很抱歉,还有其他方法可以执行此操作。
为什么要使用阴影插件并创建另一个jar容器?基于模块,已经为每个模块创建了不同的jar。
@khmarbaise,因为先前的评论认为发问者坚持在单个项目中执行,即没有模块。我同意你的看法,它应该是2个模块。