代码之家  ›  专栏  ›  技术社区  ›  ams

如何将maven模块嵌入为.class依赖项而不是jar?

  •  3
  • ams  · 技术社区  · 7 年前

    我有一个多模块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只被app使用过,不应该发布到maven repo中。我已经把应用程序分成多模块项目,因为它的应用程序中有很多代码,它的工作方式是这样的。

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

    有没有办法告诉maven模块a、B、C中编译的类应该直接插入到应用程序.jar没有生成A.JAR、B.JAR、C.JAR的类文件夹?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Jacob G.    7 年前

    我在我的多模块项目中使用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文件

    <?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
  •  0
  •   davidxxx    7 年前

    这是一个非答案。

    有没有办法告诉maven模块a中编译的类, 生产A.JAR,B.JAR,C.JAR?

    你可以用 the repackage goal of the spring boot maven plugi

    在我的例子中,模块A,B,C只被应用程序使用过,不应该被使用 发布到maven回购。

    在本地存储库中添加模块真的是希望有一个 标准 建造。
    否则,您将需要在每次运行spring boot应用程序时系统地编译每个模块。

    否则,您将被迫扭曲默认的Maven工作方式,通过添加手动任务从spring引导模块编译其他模块,并将编译后的类移到spring引导模块中。对于需要阅读/维护此配置的人来说,这真的不是一个礼物。