将多个模块的 Maven 项目组装成一个单独的 JAR 文件。

huangapple go评论71阅读模式
英文:

assembly multi modules maven project in a single jar

问题

我有一个父Maven项目,其中包含一些子项目和一个构建/汇编项目。结构如下,其中buildProj不包含任何类:

ParentProj
    + pom.xml
    + ChildProj1
        ++   pom.xml
    + ChildProj2
     ++   pom.xml
    + buildProj
     ++   pom.xml

这是buildProj的pom.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<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>gr1</groupId>
    <artifactId>buildproj</artifactId>
    <version>2.0.0</version>

    <dependencies>
        <dependency>
            <groupId>gr1</groupId>
            <artifactId>ChildProj1</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>gr1</groupId>
            <artifactId>ChildProj2</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>com.bonanza.CabalImpl</mainClass>
                            </manifest>
                        </archive>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

然而,当我运行 mvn package 命令时,buildProj的JAR文件为空。

英文:

I have a parent maven project that includes some child projects and a build/assembly project. The structure looks like this, where the buildProj does not contain any class

ParentProj
    + pom.xml 
    + ChildProj1
        ++   pom.xml
    + ChildProj2
     ++   pom.xml
    + buildProj
     ++   pom.xml

this is the pom.xml of the buildProj:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
         xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
         xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;gr1&lt;/groupId&gt;
    &lt;artifactId&gt;buildproj&lt;/artifactId&gt;
    &lt;version&gt;2.0.0&lt;/version&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;gr1&lt;/groupId&gt;
            &lt;artifactId&gt;ChildProj1&lt;/artifactId&gt;
            &lt;version&gt;2.0.0&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;gr1&lt;/groupId&gt;
            &lt;artifactId&gt;ChildProj2&lt;/artifactId&gt;
            &lt;version&gt;2.0.0&lt;/version&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;


    &lt;build&gt;
        &lt;pluginManagement&gt;
            &lt;plugins&gt;
                &lt;plugin&gt;
                    &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
                    &lt;configuration&gt;
                        &lt;archive&gt;
                            &lt;manifest&gt;
                                &lt;mainClass&gt;com.bonanza.CabalImpl&lt;/mainClass&gt;
                            &lt;/manifest&gt;
                        &lt;/archive&gt;
                        &lt;descriptorRefs&gt;
                            &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
                        &lt;/descriptorRefs&gt;
                    &lt;/configuration&gt;
                &lt;/plugin&gt;
            &lt;/plugins&gt;
        &lt;/pluginManagement&gt;
    &lt;/build&gt;



&lt;/project&gt;

nevertheless when I do mvn package, the jar of the buildProj is empty

答案1

得分: 1

尝试在版本标签下添加以下内容:

<packaging>jar</packaging>

后续编辑
如果您正在使用 assembly 插件,您还应该拥有以下 assembly XML 配置:

src/assembly/bin.xml

<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<formats>
    <format>dir</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
    <dependencySet>
        <outputDirectory>/</outputDirectory>
        <unpack>false</unpack>
        <includes>
            <include>${artifact}</include>
        </includes>
    </dependencySet>
    <dependencySet>
        <outputDirectory>/lib</outputDirectory>
        <unpack>false</unpack>
        <excludes>
            <exclude>${artifact}</exclude>
        </excludes>
    </dependencySet>
</dependencySets>
</assembly>

并使用以下命令进行打包:

mvn clean package assembly:single

同时更新您的 pom.xml 如下:

<build>
   <pluginManagement>
      <plugins>
         <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
               <archive>
                  <manifest>
                     <mainClass>com.bonanza.CabalImpl</mainClass>
                  </manifest>
               </archive>
               <descriptorRefs>
                  <descriptorRef>jar-with-dependencies</descriptorRef>
               </descriptorRefs>
            </configuration>
            <executions>
               <execution>
                  <id>make-assembly</id>
                  <phase>package</phase>
                  <goals>
                     <goal>single</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </pluginManagement>
</build>

请注意,以上内容是您提供的代码的翻译部分。如果您有任何其他疑问,请随时提问。

英文:

try to add under the version tag

&lt;packaging&gt;jar&lt;/packaging&gt;

Later edit
If you are using the assembly plugin you should also have the assembly XML

src/assembly/bin.xml

&lt;assembly
xmlns=&quot;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd&quot;&gt;
&lt;formats&gt;
    &lt;format&gt;dir&lt;/format&gt;
&lt;/formats&gt;
&lt;includeBaseDirectory&gt;false&lt;/includeBaseDirectory&gt;
&lt;dependencySets&gt;
    &lt;dependencySet&gt;
        &lt;outputDirectory&gt;/&lt;/outputDirectory&gt;
        &lt;unpack&gt;false&lt;/unpack&gt;
        &lt;includes&gt;
            &lt;include&gt;${artifact}&lt;/include&gt;
        &lt;/includes&gt;
    &lt;/dependencySet&gt;
    &lt;dependencySet&gt;
        &lt;outputDirectory&gt;/lib&lt;/outputDirectory&gt;
        &lt;unpack&gt;false&lt;/unpack&gt;
        &lt;excludes&gt;
            &lt;exclude&gt;${artifact}&lt;/exclude&gt;
        &lt;/excludes&gt;
    &lt;/dependencySet&gt;
&lt;/dependencySets&gt;

</assembly>

and to package it

mvn clean package assembly:single

also here your updated pom

&lt;build&gt;
   &lt;pluginManagement&gt;
      &lt;plugins&gt;
         &lt;plugin&gt;
            &lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
            &lt;configuration&gt;
               &lt;archive&gt;
                  &lt;manifest&gt;
                     &lt;mainClass&gt;com.bonanza.CabalImpl&lt;/mainClass&gt;
                  &lt;/manifest&gt;
               &lt;/archive&gt;
               &lt;descriptorRefs&gt;
                  &lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
               &lt;/descriptorRefs&gt;
            &lt;/configuration&gt;
            &lt;executions&gt;
               &lt;execution&gt;
                  &lt;id&gt;make-assembly&lt;/id&gt;
                  &lt;phase&gt;package&lt;/phase&gt;
                  &lt;goals&gt;
                     &lt;goal&gt;single&lt;/goal&gt;
                  &lt;/goals&gt;
               &lt;/execution&gt;
            &lt;/executions&gt;
         &lt;/plugin&gt;
      &lt;/plugins&gt;
   &lt;/pluginManagement&gt;
&lt;/build&gt;

huangapple
  • 本文由 发表于 2020年9月17日 23:49:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63941730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定