简体   繁体   English

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

[英]Maven - creating a jar for every java package in java project

lets say i have 3 packages and a jar needs to be created for every package containing only the contents in the current package.假设我有 3 个包,需要为每个只包含当前包中内容的包创建一个 jar。 My attempt is:我的尝试是:

<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>

And this does indeed create different jars, however the content inside is the same, meaning the exclude clauses don't work.这确实创建了不同的 jars,但是里面的内容是相同的,这意味着 exclude 子句不起作用。 I tried excluding only the class(relative/absolute path) and only the package.我尝试只排除类(相对/绝对路径)和包。 Don't ask why i do this its for homework, it does not make a lot of sense!不要问我为什么这样做是为了作业,它没有多大意义! This is the way i attempt of doing it, if there is any other more efficient way please feel free to share it with me!这是我尝试这样做的方式,如果还有其他更有效的方法,请随时与我分享!

EDIT: Modular structure must not be used, it must be one single project.编辑:不得使用模块化结构,它必须是一个单一的项目。

Thanks in advance提前致谢

It's already been answered in the comments but here's an example.它已经在评论中得到了回答,但这里有一个例子。

A parent pom:父 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>

And 2 child/modules poms, only the artifactId is different:和 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>

Extra info额外信息

I created the parent with the command:我使用以下命令创建了父级:

mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE

Then created 2 modules with the command然后使用命令创建了 2 个模块

mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE

If you must have a single jar project (no modules and parent) you can do it with the shade-plugin :如果您必须有一个 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>

I have accepted the above answer as right since it's the correct way to do it, but if some crazy kid like me tries to do it the wrong way, here's how:我已经接受了上述答案是正确的,因为这是正确的做法,但是如果像我这样的疯狂孩子试图以错误的方式去做,那么方法如下:

<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>

Notice to use class instead of java, since it uses the compiled extention!注意使用 class 而不是 java,因为它使用编译扩展! As for the other files, they are resources on the classpath至于其他文件,它们是类路径上的资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM