【问题标题】:How to embed maven modules as .class dependencies as opposed to jars?如何将 maven 模块嵌入为 .class 依赖项而不是 jars?
【发布时间】:2018-08-13 19:27:02
【问题描述】:

我有一个多模块 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 文件来创建一个 JAR 文件 - 但这并不是你想要的。 ..
  • 问题是:将A、B、C jar 部署到maven repo 有什么问题?您正在部署生成的 app.jar 吗?远离配置而不是约定会使维护这些东西变得更加困难,并且让其他人更难理解......

标签: java maven


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    这里没有答案。

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

    您可以使用the repackage goal of the spring boot maven plugin 将依赖项扁平化为 uber jar 中的类。

    在我的情况下,模块 A、B、C 仅由应用程序使用,不应该 发布到 maven repo 中。

    在本地存储库中添加模块确实需要高效标准构建。
    否则,您将需要在每次要运行 Spring Boot 应用程序时系统地编译每个模块。
    虽然实际上有时您需要构建依赖项,但有时您不需要,因为这些已经更新。
    否则,您将受限于通过添加手动任务以从 spring boot 模块编译其他模块并将编译的类移动到 spring boot 模块来扭曲默认的 Maven 工作方式。对于必须阅读/维护此配置的人来说,这真的不是一份礼物。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-27
      • 2021-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 2011-07-31
      相关资源
      最近更新 更多