简体   繁体   中英

Using maven assembly plugin to create ZIP file containing a fat jar (jar-with-dependencies)

I am using the maven assembly-plugin and I want to create a ZIP file, which among other files contains a fat jar (jar-with-dependencies). I cannot create this in one run using mvn package . I can either uncomment the configuration for the descriptor or uncomment the jar-with-dependencies part.

The build section of the pom.xml is as follows:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/main/assembly/dist.xml</descriptor>
                </descriptors>
                <finalName>sample-documentum-downloader</finalName>
            </configuration>

            <executions>
                <execution>
                    <id>jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.artifactId}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <mainClass>com.foo.DownloadDocuments</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>

                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

and the dist.xml is:

<assembly>
<id>dist</id>
<formats>
    <format>zip</format>
</formats>
<files>
    <file>
        <source>target/${project.artifactId}.jar</source>
        <outputDirectory>/</outputDirectory>
    </file>
    <file>
        <source>data/input/docId.txt</source>
        <outputDirectory>data/input/</outputDirectory>
    </file>
    <file>
        <source>data/export/exported_files_will_be_created_here.txt</source>
        <outputDirectory>data/export/</outputDirectory>
    </file>
    <file>
        <source>src/main/resources/dfc.properties</source>
        <outputDirectory>/</outputDirectory>
    </file>
    <file>
        <source>src/main/resources/dfc.keystore</source>
        <outputDirectory>/</outputDirectory>
    </file>

</files>
<fileSets>
    <fileSet>
        <directory>${project.basedir}</directory>
        <includes>
            <include>*.cmd</include>
            <include>README.pdf</include>
        </includes>
        <useDefaultExcludes>true</useDefaultExcludes>
    </fileSet>
</fileSets>

How can I restructure to create a ZIP including the far JAR in one run.

Thanks for your support.

Thanks to Tunaki, my build section now looks like this:

  <build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>jar-with-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <finalName>${project.artifactId}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <mainClass>com.foo.DownloadDocuments</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </execution>

                <execution>
                    <id>assemble-all</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/dist.xml</descriptor>
                        </descriptors>
                        <finalName>sample-documentum-downloader</finalName>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

The problem is that you declared the <configuration> element global to the plugin. This means that all executions will inherit that configuration: assemble-all but also jar-with-dependencies . As such, the inherited <descriptors> is probably messing with the <descriptorRefs> .

You need to move that <configuration> element to the specific assemble-all execution, just like you did for the jar-with-dependencies execution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM