简体   繁体   English

从多个Maven模块构建可执行jar

[英]Build executable jar from multiple maven modules

I have problem with building the single executable jar from multiple maven modules. 我从多个Maven模块构建单个可执行jar时遇到问题。 So here is the situation: I have three maven modules: app -> persistence -> domain. 因此,情况如下:我有三个Maven模块:app->持久性->域。 I have parent pom.xml too. 我也有父pom.xml。 I added to this parent pom maven assembly plugin: 我添加到此父pom maven程序集插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
            <manifest>
                <mainClass>com.toys.app.Service</mainClass>
            </manifest>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
</plugin>

So when I type from command line in parent pom directory (which contain parent.pom and app, persistence and domain maven modules) command: mvn assembly:assembly it gave my next error: 因此,当我从父pom目录(包含parent.pom和应用程序,持久性和域maven模块)中的命令行键入命令时, mvn assembly:assembly给出了我的下一个错误:

[INFO] ------------------------------------------------------------------------
[INFO] Building persistence 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-assembly-plugin:2.2-beta-5:single (default-cli) @ persistence ---
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] parent ..................................... SUCCESS [3.147s]
[INFO] domain ............................................ SUCCESS [4.765s]
[INFO] persistence ....................................... FAILURE [0.570s]
[INFO] app ............................................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.287s
[INFO] Finished at: Wed Sep 28 12:30:26 CEST 2011
[INFO] Final Memory: 6M/81M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-assembly-plugin:2.2-beta-5:single (default-cli) on project persistence: Failed to create assembly: Failed to resolve dependencies for project: com.toys:persistence:jar:1.0-SNAPSHOT: Missing:
[ERROR] ----------
[ERROR] 1) com.toys:domain:jar:1.0-SNAPSHOT
[ERROR] 
[ERROR] Try downloading the file manually from the project website.
[ERROR] 
[ERROR] Then, install it using the command:
[ERROR] mvn install:install-file -DgroupId=com.toys -DartifactId=domain -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file
[ERROR] 
[ERROR] Alternatively, if you host your own repository you can deploy the file there:
[ERROR] mvn deploy:deploy-file -DgroupId=com.toys -DartifactId=domain -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]
[ERROR] 
[ERROR] Path to dependency:
[ERROR] 1) com.toys:persistence:jar:1.0-SNAPSHOT
[ERROR] 2) com.toys:domain:jar:1.0-SNAPSHOT
[ERROR] 
[ERROR] ----------
[ERROR] 1 required artifact is missing.
[ERROR] 
[ERROR] for artifact:
[ERROR] com.toys:persistence:jar:1.0-SNAPSHOT
[ERROR] 
[ERROR] from the specified remote repositories:
[ERROR] central (http://repo1.maven.org/maven2, releases=true, snapshots=false)
[ERROR] -> [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
[ERROR] 
[ERROR] After correcting the problems, you can resume the build with the command
[ERROR]   mvn <goals> -rf :persistence

Every other maven life-cycles works fine! 其他所有Maven生命周期都可以正常工作! Can Anyone help me? 谁能帮我?

Try to add execution phase: 尝试添加执行阶段:

      <plugin>
          <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
                <archive>
                  <manifest>
                    <mainClass>com.toys.app.Service</mainClass>
                  </manifest>
                </archive>               
              <descriptorRef>jar-with-dependencies</descriptorRef>                  
          </configuration>
          <executions>
                <execution>
                  <id>make-jar-with-dependencies</id>
                  <phase>package</phase>
                  <goals>
                      <goal>single</goal>
                  </goals>
              </execution>
          </executions>
        </plugin>
    </plugins>

I suggest you add project 'distribution' on the same level as your app, persistence, domain projects. 我建议您在与应用程序,持久性,域项目相同的级别上添加项目“分布”。

<project>

  <artifactId>distribution</artifactId>
  <packaging>pom</packaging>

  <parent>
    ...
  </parent>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>app</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>persistence</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>domain</artifactId>
      <version>${project.version}</version>
      <scope>runtime</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-assembly-plugin</artifactId>
          <configuration>
            <archive>
              <manifest>
                <mainClass>com.toys.app.Service</mainClass>
              </manifest>
            </archive>               
            <descriptorRef>jar-with-dependencies</descriptorRef>                  
          </configuration>
          <executions>
            <execution>
              <id>make-jar-with-dependencies</id>
              <phase>package</phase>
              <goals>
                <goal>single</goal>
              </goals>
            </execution>
          </executions>
        <plugin>
    </plugins>
  </build>

</project>

I had similar problems when trying to create distribution in the reactor POM, after moving to its own project - no headaches. 在转到自己的项目后,尝试在反应堆POM中创建分配时遇到了类似的问题-不用担心。

Also, don't forget to add 'distribution' module to the reactor project. 另外,不要忘了“分配”模块添加到反应堆项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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