java - 如何将 Maven 模块作为 .class 依赖项而不是 jar 嵌入?

标签 java maven

我有一个多模块 maven 项目,用于生成单个 spring boot fat jar。我的项目看起来像这样。

 - Parent Module Aggergator 
    - A 
    - B
    - C 
    - app <-- app.jar is the only thing I want to publish 

在我的案例中,模块 A、B、C 仅由应用程序使用,不应发布到 Maven 存储库中。我已将应用程序拆分为多模块项目,因为应用程序中有很多代码,并且需要以这种方式工作。

目前 app.jar 将包含 a.jar, b.jar c.jar

有没有办法告诉 maven 从模块 A、B、C 编译的类应该只插入到 app.jar 类文件夹中,而无需生成 A.JAR、B.JAR、C.JAR?

最佳答案

我将 Maven Shade 插件用于我的多模块项目;它创建一个 JAR 并将每个模块提取到其中,而不是创建多个 JAR 文件:

pom.xml:

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

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <groupId>...</groupId>
    <artifactId>pipeline</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <modules>
        <module>firehose</module>
        <module>gson</module>
        <module>lambda</module>
        <module>mapper</module>
        <module>model</module>
        <module>receiver</module>
        <module>redshift</module>
        <module>reloader</module>
        <module>s3</module>
        <module>sns</module>
        <module>sqs</module>
        <module>systemstests</module>
        <module>transaction</module>
        <module>utility</module>
    </modules>

    <dependencies>
        ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

pom.xml(JAR):

<?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>
        <groupId>...</groupId>
        <artifactId>pipeline</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>lambda</artifactId>

    <dependencies>
        ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                   <finalName>MyJar</finalName>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

关于java - 如何将 Maven 模块作为 .class 依赖项而不是 jar 嵌入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51829293/

相关文章:

java - 在 Android 中加载到内存中和数据库中的逻辑

java - 无法捆绑适用于 Mac 的 Java 应用程序 1.8

java - 在 Maven 中,如何自定义生命周期阶段?

java - 如何使用 maven 配置 pom.xml 以集成 install4j 安装程序中的所有依赖库

java - 不理解模数的工作原理(Eclipse)

java - 简单的 websocket 和静态文件示例

java - Spring JDBCTemplate : Construct JSON without special characters

java - 无法使用 wagon maven FTPS 传输中型大文件

java - 无法使用发布插件标记问题

java - 从请求体获取params,但不能在mybatis注解连接查询中使用