【问题标题】:Maven Assembly plugin doesnt copy jar-with-dependences to the zip fileMaven Assembly 插件不会将 jar-with-dependencies 复制到 zip 文件
【发布时间】:2015-12-14 07:56:50
【问题描述】:

我执行“mvn clean package”,它使用snapshot.jar 创建一个压缩文件(基于我的assembly.xml),然后创建压缩文件,最后创建jar-with-dependencies.jar

我认为因为 zip 文件是在创建 jar-with-dependencies.jar 之前创建的,所以它从未被复制到 zip 文件中。

如何强制 maven 将 snapshot.jarjar-with-dependencies.jar 复制到 zip 文件中(换句话说,如何强制在调用组装目标之前先构建 jar-with-dependencies,以便所有 jar复制到压缩文件)

这里是 pom.xml

    <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.mycomonay</groupId>
<artifactId>artid</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>art</name>
<description>artdescription</description>
<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.16.6</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.4.3</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>19.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>

</dependencies>
<build>
    <plugins>
        <!-- <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> 
            <version>2.4</version> <configuration> <archive> <manifestEntries> <Built-By>xyz</Built-By> 
            <Class-Path>.</Class-Path> </manifestEntries> </archive> </configuration> 
            </plugin> -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgument>-Xlint:unchecked</compilerArgument>
            </configuration>

        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4</version>

            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifestEntries>
                        <Built-By>abc</Built-By>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                    <manifest>
                        <!-- <addClasspath>true</addClasspath> -->

                        <mainClass>com.mycomonay.Main</mainClass>
                    </manifest>
                </archive>


                <descriptors>
                    <descriptor>src/main/assembly/assembly.xml</descriptor>

                </descriptors>
            </configuration>
                <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- <resources> <resource> <directory>src/main/resources</directory> </resource> 
        </resources> -->
</build>

这里是 assembly.xml

           <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
          <id>bin</id>
        <!--  <baseDirectory>/</baseDirectory> -->
            <formats>
      <format>zip</format>
          </formats>
       <includeBaseDirectory>false</includeBaseDirectory>
       <fileSets>
          <fileSet>
             <directory>${project.build.directory}</directory>
             </fileSet>
           <fileSet>
            <directory>${project.basedir}/scripts</directory>
               <outputDirectory>scripts</outputDirectory>
              <includes>
                  <include>*</include>
               </includes>
             </fileSet>
<fileSet>
        <directory>${project.build.directory}</directory>
        <outputDirectory>bin</outputDirectory>
        <includes>
            <include>*with-dependencies.jar</include>
        </includes>
    </fileSet>

【问题讨论】:

  • 您应该包含pom.xml,否则您不能指望人们能够全力帮助您。
  • 在问题末尾添加了 pom.xml 和 assembly.xml

标签: maven maven-3


【解决方案1】:

您绝对可以将single 目标分成两个执行(参见https://stackoverflow.com/a/8726969/3114959https://stackoverflow.com/a/15799254/3114959)。
Maven 按照它们在有效 pom 中出现的顺序执行事物(只要属于同一阶段),所以如果你指定两个执行分开,你应该没问题。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
                <execution>
                    <id>create-jar-with-dependencies</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </execution>
                <execution>
                    <id>make-the-zip</id>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <phase>package</phase>
                    <configuration>
                        <descriptors>
                            <descriptor>src/main/assembly/assembly.xml</descriptor>
                        </descriptors>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

因此,您将在构建期间看到 2 次执行:

[INFO] --- maven-assembly-plugin:2.2-beta-5:single (create-jar-with-dependencies) @ artid ---
[INFO] ... some more output ...
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (make-the-zip) @ artid ---

后一个执行(make-the-zip)肯定可以包含前一个(create-jar-with-dependencies)的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-13
    • 2013-03-13
    相关资源
    最近更新 更多