有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Maven为java项目中的每个java包创建一个jar

假设我有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,但是里面的内容是相同的,这意味着排除子句不起作用。我试图只排除类(相对/绝对路径)和包。别问我为什么这么做是为了做作业,这没什么意义! 这是我尝试的方法,如果有其他更有效的方法,请随时与我分享

编辑:不能使用模块化结构,它必须是一个单独的项目

提前谢谢


共 (2) 个答案

  1. # 1 楼答案

    评论中已经给出了答案,但这里有一个例子

    父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个子模块POM,只有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
    

    然后用命令创建了两个模块

    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>
    
  2. # 2 楼答案

    我已经接受了上面的答案,因为这是正确的方法,但如果像我这样的疯子试图用错误的方法来做,以下是方法:

    <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>
                                <includes>
                                    <include>first/FirstMain.class</include>
                                    <include>log4j.properties</include>
                                    <include>pictures/12px-Commons-logo.svg.png</include>
                                    <include>textFiles/first.txt</include>
                                </includes>
                            </configuration>
                        </execution>
        </plugin>
    
    

    请注意使用类而不是java,因为它使用的是已编译的扩展名!至于其他文件,它们是类路径上的资源