首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Maven -为java项目中的每个java包创建一个jar。

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

Stack Overflow用户
提问于 2020-01-30 13:35:02
回答 2查看 389关注 0票数 2

假设我有3个包,需要为每个只包含当前包中内容的包创建一个jar。我的尝试是:

代码语言:javascript
运行
复制
<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>

这确实会创建不同的jars,但是内部的内容是相同的,这意味着排除子句不起作用。我尝试只排除类(相对/绝对路径)和包。别问我为什么这么做是为了家庭作业,这没什么意义!这是我尝试的方式,如果有其他更有效的方法,请随时与我分享!

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

提前感谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-30 13:50:04

它已经在评论中得到了回答,但下面是一个例子。

父母的pom:

代码语言:javascript
运行
复制
<?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>

和两个子/模块poms,只有artifactId不同:

代码语言:javascript
运行
复制
<?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>

额外信息

我使用以下命令创建了父级:

代码语言:javascript
运行
复制
mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE

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

代码语言:javascript
运行
复制
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE

如果您必须有一个jar项目(没有模块和父项目),您可以使用遮阳插件完成它。

代码语言:javascript
运行
复制
<?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
EN

Stack Overflow用户

发布于 2020-01-31 08:02:25

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

代码语言:javascript
运行
复制
<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,因为它使用编译后的扩展!至于其他文件,它们是类路径上的资源。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59987286

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档