簡體   English   中英

Maven程序集:捆綁文檔和源jar

[英]Maven assembly : bundle docs and sources jars

我有一個多模塊的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-plugin 文檔中的說明在Distribution模塊中組裝其他模塊的二進制文件 我的POM外觀與本教程中的完全相同。

它以文檔中顯示的方式工作。 在打包階段,程序集插件將兩個其他模塊的jar收集到Distribution模塊中。 但正如您所看到的,其他庫模塊也已配置為生成文檔和源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)

好吧,我不是專家,但我在多模塊項目中也做類似的事情。 這是父母的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>

我不確定,您真正需要什么,但是也許您可以看一下創建站點的文檔。

產生文件

迷你指南創建網站

這是一個很大的解釋,希望對您有所幫助。

好的,我經歷了以下選擇:

  1. maven-assembly-plugin :構建組合的二進制文件,也可能是源文件,但沒有javadocs。 另外,非常復雜。
  2. maven-shade-plugin :非常容易地構建組合的二進制文件和源代碼,但是沒有javadocs。 另外,源文件名很難控制。

因此,我采用的簡單方法如下:

確保所有模塊都生成源jar。 這可以通過啟用父pom中的source插件來完成,該插件由所有模塊繼承:

<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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM