【问题标题】:Maven assembly : bundle docs and sources jarsMaven 程序集:捆绑文档和源 jars
【发布时间】:2013-12-04 16:50:32
【问题描述】:

我有一个多模块 Maven 项目。每个库模块都会生成自己的 jar 输出:

Parent (Pom)
   |
   Library_01 (jar)
   |   output:
   |     |
   |     lib1.jar
   |     |
   |     lib1-docs.jar
   |     |
   |     lib1-sources.jar
   |
   Library_02 (jar)
   |   output:
   |     |
   |     lib2.jar
   |     |
   |     lib2-docs.jar
   |     |
   |     lib2-sources.jar
   |
   Distribution (pom) // uses assembly plug-in to assemble outputs of other modules here
       output:
         |
         lib1.jar
         |
         lib2.jar

我正在分发模块中组装其他模块的二进制文件,完全按照maven-assembly-plugindocumentation here 中的说明进行。我的 POM 看起来与教程中的完全一样。

它按照文档中显示的方式工作。在打包阶段,程序集插件在分发模块中收集其他模块的 jar。但正如您所见,其他库模块也被配置为生成文档和源 jar。

  1. 如何配置程序集插件以收集其他模块的文档和源 jar?

  2. 考虑分发,上述设置似乎不合适。我最终还是得到了一堆用于不同库的罐子。可以将所有其他模块的类、源代码和文档各自组合成一个 jar 文件吗?基本上,我想要分发 3 个全局 jar(二进制、文档和源)。如何做到这一点?

喜欢:

   |
   Distribution (pom)
         |
         Global.jar (contains classes of Lib1 + Lib2)
         |
         Global-docs.jar (contains docs of Lib1 + Lib2)
         |
         Global-sources.jar (contains sources of Lib1 + Lib2)

【问题讨论】:

    标签: java maven


    【解决方案1】:

    好吧,我不是专家,但我也在多模块项目中做类似的事情。 这是一个家长的 pom 片段,其中指定了插件和文档:

    <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <inherited>true</inherited>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>jar</goal>
                                <goal>test-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <inherited>true</inherited>
                    <configuration>
                        <properties>
                            <property>
                                <name>outputDirectory</name>
                                <value>${project.build.outputDirectory}</value>
                            </property>
                        </properties>
                        <systemProperties>
                            <property>
                                <name>user.language</name>
                                <value>ca</value>
                            </property>
                            <property>
                                <name>user.country</name>
                                <value>ES</value>
                            </property>
                        </systemProperties>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.0</version>
                    <inherited>false</inherited>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <inherited>true</inherited>
                    <configuration>
                        <executable>javac</executable>
                        <source>1.6</source>
                        <target>1.6</target>
                        <encoding>ISO-8859-15</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <version>2.8</version>
                    <inherited>true</inherited>
                    <configuration>
                        <detectLinks>true</detectLinks>
                    </configuration>
                    <executions>
                        <execution>
                            <id>aggregate</id>
                            <goals>
                                <goal>aggregate</goal>
                            </goals>
                            <phase>site</phase>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <inherited>true</inherited>
                    <configuration>
                        <encoding>ISO-8859-15</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-ejb-plugin</artifactId>
                    <version>2.2.1</version>
                    <inherited>true</inherited>
                    <configuration>
                        <ejbVersion>3.0</ejbVersion>
                        <archive>
                            <addMavenDescriptor>false</addMavenDescriptor>
                            <manifest>
                                <addClasspath>true</addClasspath>
                                <useUniqueVersions>false</useUniqueVersions>
                            </manifest>
                        </archive>
                        <generateClient>false</generateClient>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <inherited>true</inherited>
                    <configuration>
                        <archive>
                            <addMavenDescriptor>false</addMavenDescriptor>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>test-jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.0</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <!-- <configuration> <links> <link>http://download.oracle.com/javase/6/docs/api/</link> 
                    <link>http://download.oracle.com/javaee/1.4/api/</link> <link>http://static.springsource.org/spring/docs/2.5.x/api/</link> 
                    <link>http://docs.jboss.org/hibernate/core/3.2/api/</link> <link>http://www.easymock.org/api/easymock/2.5.2/</link> 
                    <link>http://testng.org/javadocs/</link> <link>http://www.bouncycastle.org/docs/docs1.6/</link> 
                    <link>http://www.bouncycastle.org/docs/mdocs1.6/</link> <link>http://www.bouncycastle.org/docs/pgdocs1.6/</link> 
                    <link>http://www.bouncycastle.org/docs/tspdocs1.6/</link> </links> </configuration> -->
                <executions>
                    <execution>
                        <id>agregar</id>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                        <phase>pre-site</phase>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <reporting>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>2.1.1</version>
            </plugin>
        </plugins>
    </reporting>
    

    【讨论】:

    • 你定义了两次javadoc插件?
    【解决方案2】:

    好的,我已经通过了以下选项:

    1. maven-assembly-plugin :构建组合二进制文件,也可能是源,但没有 javadocs。而且,非常复杂。
    2. maven-shade-plugin :非常容易构建组合的二进制文件和源代码,但没有 javadocs。此外,源文件名难以控制。

    所以,我采取的简单方法如下:

    确保所有模块都生成源 jar。这可以通过在父 pom 中启用源插件来完成,该插件由所有模块继承:

    <build>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <artifactId>maven-source-plugin</artifactId>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    

    创建一个特殊的“构建”模块来组合所有其他模块的源。这是通过使用maven-dependency-pluginunpack-dependencies 目标和源分类器来完成的。这意味着将所有其他模块源合并为该模块的源。

    构建模块的 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>
    
        <parent>
            <artifactId>parent</artifactId>
            <groupId>com.example.product</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>build</artifactId>
        <packaging>jar</packaging>
        <name>Build</name>
    
        <properties>
            <!--add comma separated artifact names from dependencies, to be included in unpacking-->
            <project.build.unpack_includes>sub-product</project.build.unpack_includes>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>sub-product</artifactId>
                <version>${project.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <finalName>${parent.artifactId}</finalName>
            <plugins>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>unpack-sources</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>unpack-dependencies</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <classifier>sources</classifier>
                        <type>jar</type>
                        <outputDirectory>${project.build.sourceDirectory}</outputDirectory>
                        <includeArtifactIds>${project.build.unpack_includes}</includeArtifactIds>
                        <includes>**\/*.java</includes>
                        <overWriteSnapshots>true</overWriteSnapshots>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-javadoc-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>attach-javadocs</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    </project>
    

    现在,构建模块像任何其他项目一样编译,生成所有必需的 jar。

    【讨论】:

      猜你喜欢
      • 2014-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-28
      • 1970-01-01
      相关资源
      最近更新 更多