Maven包装了几个带阴影的罐子

时间:2018-06-15 05:35:59

标签: java maven zip

我目前正在运行一个多模块maven项目,在每个模块中我都会生成一个阴影jar。这些jar用于分别运行,因为它们代表三个不同的Spark作业。

然而,想要制造"运输"他们在某个地方更容易,我想把所有这些阴影罐子拉上来。我已经尝试使用汇编插件来实现这一目标,但我一直在努力。

当我尝试使用模块集时,似乎抓住了无阴影的jar。

我已经能够使用文件集来实现这一点,但它不是很优雅,而且它没有处理我有开发和生产构建改变阴影命名这一事实广口瓶中。

我假设模块集仍然是正确的方法,但我不知道如何让它抓住我的阴影罐和阴影罐。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

根据示例herehere

,您需要这样的内容

1)带有阴影jar项目的父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.greg</groupId>
    <artifactId>assembly-example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>pom</packaging>

    <name>assembly-example</name>

    <modules>
        <module>shaded-jar-1</module>
        <module>shaded-jar-2</module>
        <module>distribution</module>
    </modules>

</project>

2)这样的阴影罐子项目我用作例子

<?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>assembly-example</artifactId>
    <groupId>com.greg</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>shaded-jar-1</artifactId>

  <name>shaded-jar-1</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greg.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

3)将构建最终zip的分发项目

<?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>assembly-example</artifactId>
        <groupId>com.greg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>distribution</artifactId>

    <packaging>pom</packaging>

    <name>Distribution</name>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <id>distro-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <configuration>
                            <descriptors>
                                <descriptor>src/assembly/bin.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

4)用于控制zip构建的bin.xml

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">

    <id>bin</id>

    <formats>
        <format>zip</format>
    </formats>

    <includeBaseDirectory>false</includeBaseDirectory>

    <moduleSets>
        <moduleSet>

            <!-- Enable access to all projects in the current multimodule build! -->
            <useAllReactorProjects>true</useAllReactorProjects>

            <!-- Now, select which projects to include in this module-set. -->
            <includes>
                <include>com.greg:shaded-jar-1</include>
                <include>com.greg:shaded-jar-2</include>
            </includes>
            <binaries>
                <outputDirectory>modules/maven-assembly-plugin</outputDirectory>
                <unpack>false</unpack>
            </binaries>
        </moduleSet>
    </moduleSets>

</assembly>

这将为您提供一个带有胖罐的拉链

jar tvf distribution/target/distribution-1.0-SNAPSHOT-bin.zip 
     0 Fri Jun 15 10:37:32 BST 2018 modules/
     0 Fri Jun 15 10:37:32 BST 2018 modules/maven-assembly-plugin/
501520 Fri Jun 15 10:37:30 BST 2018 modules/maven-assembly-plugin/shaded-jar-1-1.0-SNAPSHOT.jar
501520 Fri Jun 15 10:37:30 BST 2018 modules/maven-assembly-plugin/shaded-jar-2-1.0-SNAPSHOT.jar

检查你的阴影插件是否正常工作,在traget文件夹中你应该有一个胖罐子和一个原始罐子,胖罐子是'defaut'神器。

greg@greg-XPS-13-9360:~/work/assembly-example$ ls -l shaded-jar-1/target/
total 520
drwxrwxr-x 3 greg greg   4096 Jun 15 10:37 classes
drwxrwxr-x 3 greg greg   4096 Jun 15 10:37 generated-sources
drwxrwxr-x 3 greg greg   4096 Jun 15 10:37 generated-test-sources
drwxrwxr-x 2 greg greg   4096 Jun 15 10:37 maven-archiver
drwxrwxr-x 3 greg greg   4096 Jun 15 10:37 maven-status
-rw-rw-r-- 1 greg greg   2418 Jun 15 10:37 original-shaded-jar-1-1.0-SNAPSHOT.jar
-rw-rw-r-- 1 greg greg 501520 Jun 15 10:37 shaded-jar-1-1.0-SNAPSHOT.jar
drwxrwxr-x 3 greg greg   4096 Jun 15 10:37 test-classes

答案 1 :(得分:0)

在与您现有模块相同级别的module1模块中(此处为module2src/assembly/bin.xml,使用fileSets而不是{{ 1}}:

moduleSets

分发项目的<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd"> <id>bin</id> <formats> <format>dir</format> <format>zip</format> </formats> <includeBaseDirectory>false</includeBaseDirectory> <fileSets> <fileSet> <directory>../module1/target</directory> <includes> <include>*-shaded.jar</include> </includes> <outputDirectory></outputDirectory> </fileSet> <fileSet> <directory>../module2/target</directory> <includes> <include>*-shaded.jar</include> </includes> <outputDirectory></outputDirectory> </fileSet> </fileSets> </assembly> 与往常相同:

pom.xml