maven-assembly-plugin MojoExecutionException with dependencySet as outputDirectory

2022-09-03 13:23:02

在我的Khatami项目中,我使用maven来管理编译并将结果打包成一个可运行的工件:顶层的可执行shell脚本,包含可执行jar及其依赖jar。请明白我在这里的意思。bin/

作为参考,以下是Khatami的pom的突出部分.xml:

      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <configuration>
          <descriptors>
            <descriptor>src/main/assembly/src.xml</descriptor>
          </descriptors>
          <archive>
            <manifest>
              <mainClass>${project.groupId}.Main</mainClass>
            </manifest>
          </archive>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>

和完整的 src/main/assembly/src.xml

<assembly>
  <id>dist</id>
  <formats>
    <format>tar.gz</format>
  </formats>
  <dependencySets>
    <dependencySet>
      <outputDirectory>bin</outputDirectory>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>src/main/assembly</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>khatami</include>
      </includes>
      <fileMode>744</fileMode>
      <lineEnding>unix</lineEnding>
      <filtered>true</filtered>
    </fileSet>
  </fileSets>
</assembly>

和编译尝试:

$ mvn clean compile assembly:single
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building khatami 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ khatami ---
[INFO] Deleting /home/blt/projects/com/carepilot/repos/khatami/target
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ khatami ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/blt/projects/com/carepilot/repos/khatami/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ khatami ---
[INFO] Compiling 1 source file to /home/blt/projects/com/carepilot/repos/khatami/target/classes
[INFO] 
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (default-cli) @ khatami ---
[INFO] Reading assembly descriptor: src/main/assembly/src.xml
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.721s
[INFO] Finished at: Mon Jul 18 13:58:30 EDT 2011
[INFO] Final Memory: 8M/123M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project khatami: Failed to create assembly: Error adding file 'com.carepilot.khatami:khatami:jar:1.0-SNAPSHOT' to archive: /home/blt/projects/com/carepilot/repos/khatami/target/classes isn't a file. -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

我的错在哪里?


答案 1

错误信息的相关部分是

Error adding file 'com.carepilot.khatami:khatami:jar:1.0-SNAPSHOT' to archive:
/home/blt/projects/com/carepilot/repos/khatami/target/classes isn't a file.

它正在等待一个文件,但它找不到它,因为目标没有在.packageclean

如果你这样做,它将成功构建。mvn clean compile package assembly:single

我会将目标添加到阶段,这样它将自动构建。assembly:singlepackage

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>src/main/assembly/src.xml</descriptor>
        </descriptors>
        <archive>
          <manifest>
            <mainClass>${project.groupId}.Main</mainClass>
          </manifest>
        </archive>
        <descriptorRefs>
          <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
      </configuration>
    </execution>
  </executions>
</plugin>

通过对配置的上述更改,您可以发布。

mvn clean package

目标将自动执行。assembly:single

更好的方法可能是使用maven-shade-plugin而不是手动执行此操作。


答案 2

您的问题:

Error adding file 'com.carepilot.khatami:khatami:jar:1.0-SNAPSHOT' to archive:
/home/blt/projects/com/carepilot/repos/khatami/target/classes isn't a file.

是由于项目工件 (com.carepilot.khatami:khatami:jar:1.0-SNAPSHOT) 尚未打包,但属于程序集的 .您可以添加到中以解决问题。<dependencySet><useProjectArtifact>false</useProjectArtifact><dependencySet>

如果需要程序集中包含项目的类文件,则可以使用“包含”目录 。<fileSet>target/classes

例如:

<assembly>
  <id>dist</id>
  <formats>
    <format>tar.gz</format>
  </formats>
  <dependencySets>
    <dependencySet>
      <useProjectArtifact>false</useProjectArtifact>
      <outputDirectory>bin</outputDirectory>
    </dependencySet>
  </dependencySets>
  <fileSets>
    <fileSet>
      <directory>src/main/assembly</directory>
      <outputDirectory>/</outputDirectory>
      <includes>
        <include>khatami</include>
      </includes>
      <fileMode>744</fileMode>
      <lineEnding>unix</lineEnding>
      <filtered>true</filtered>
    </fileSet>
  </fileSets>
</assembly>

另一种选择是将程序集附加到 之后的某个阶段。packaging

编辑:我也在 Eclipse 中看到了这个问题,当在 Maven 构建设置中启用时。Resolve Workspace artifacts


推荐