如何为maven父项目生成jar

Kal*_*sto 17 parent pom.xml maven

Maven需要一个父项目

<packaging>pom</packaging>

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

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

noa*_*hlz 13

使用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>
Run Code Online (Sandbox Code Playgroud)

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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

关于Maven实践的说明:

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

  • 感谢noahz,感谢您的解决方案.我想为父项目生成jar文件,以避免在Eclipse Workspace中出现额外的空项目,从而简化项目关系.我是maven的新手,所以我可能错了.您是否建议使用空父项目?我怀疑你的解决方案会冲突儿童项目,这些项目都有自己的jar构建插件. (2认同)
  • 拥有一个"空"(不产生工件)的父项目完全没问题,因为它只用于定义子项目使用的属性设置,依赖项版本,插件等.顺便说一句 - 如果您使用我的解决方案,请单击复选标记接受答案. (2认同)