如何为maven父项目生成jar

时间:2012-09-06 17:18:32

标签: maven parent pom.xml

Maven需要父项目

<packaging>pom</packaging>

父pom.xml中的

子句。安装这样的项目时,只有一个pom文件生成到maven存储库中。无论父项目是否具有任何Java代码,都不会生成Jar文件。这迫使我有额外的空父项目,这是过度的。从逻辑上讲,我的一些图书馆可能同时是父母。

有没有办法为父项目生成pom和jar文件,而无需在安装之间删除/添加packaging子句?

1 个答案:

答案 0 :(得分:12)

使用Maven Jar插件和Maven Build Helper。示例POM:

<?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>test</groupId>
  <artifactId>test</artifactId>
  <version>1.0</version>

  <packaging>pom</packaging>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>build-helper-maven-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <id>attach-artifacts</id>
            <phase>package</phase>
            <goals>
              <goal>attach-artifact</goal>
            </goals>
            <configuration>
              <artifacts>
                <artifact>
                  <file>test-${project.version}</file>
                  <type>jar</type>
                </artifact>
              </artifacts>
            </configuration>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.4</version>
        <executions>
          <execution>
            <id>default</id>
            <goals>
              <goal>jar</goal>
            </goals>
            <phase>package</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Maven构建结果:

mvn install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building test 1.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- build-helper-maven-plugin:1.7:attach-artifact (attach-artifacts) @ test ---
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default) @ test ---
[WARNING] JAR will be empty - no content was marked for inclusion!
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ test ---
[INFO] Installing /home/username/projects/test/pom.xml to /home/username/.m2/repository/test/test/1.0/test-1.0.pom
[INFO] Installing /home/username/projects/test/test-1.0 to /home/username/.m2/repository/test/test/1.0/test-1.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.805s
[INFO] Finished at: Thu Sep 06 13:33:20 EDT 2012
[INFO] Final Memory: 4M/119M
[INFO] ------------------------------------------------------------------------

关于Maven做法的说明:

父模块通常用于定义所有子模块共同使用的依赖项和插件。它很少有自己的输出。您可能希望有一个“分发”子模块来聚合所有其他模块工件,而不是尝试在父模块中执行它。