我正在使用maven assembly plug in打包我的项目及其所有依赖项,因此我可以运行一个简单的java -jar myproject.jar并能够运行该项目。但是当我跑 jar 时,它告诉我

Error: Could not find or load main class com.project.ServerStart

然后,我解压缩了.jar文件,发现程序集不包含我的项目文件,这太荒谬了!
在打包项目时,我会收到此警告
[WARNING] Cannot include project artifact: Amjar:amjar:pom:0.2; it doesn't have an associated file or directory.

这是我的插件配置
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <finalName>amjar-${project.version}</finalName>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>com.project.ServerStart</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

我究竟做错了什么 ?

最佳答案

根据您在问题中提出的警告:

[WARNING] Cannot include project artifact: Amjar:amjar:pom:0.2; it doesn't have an associated

我猜想您的packaging中的pom.xml设置为pom

如果您的模块只是打包一组依赖项或资源文件,这很好,但是,如果您还希望Maven在模块中创建一个包含类的jar,则需要将packaging设置为jar

关于java - 为什么Maven Assembly Plugin不在具有依赖项的jar中包含我的项目文件?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23777934/

10-14 11:29
查看更多