maven:如何在“包”阶段用jar导出打包源

时间:2013-09-30 10:22:49

标签: maven

我有一个maven导出脚本(我没有写过),我也希望添加源代码。 构建脚本使用“maven-source-plugin”生成2个输出: .jar和-sources.jar并且它们都存在于彼此相邻的同一输出文件夹中。 到目前为止只复制了jar,我希望脚本将-sources.jar文件放在它的jar文件旁边

构建pom:

<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>org.mytest</groupId>
    <artifactId>my-parent</artifactId>
    <version>6.0.00-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>my-parent</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <assembly.format>dir</assembly.format>
        <my.repository.rootUrl>http://maven.my.com</my.repository.rootUrl>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            .
            .
            .
        </dependencies>
    </dependencyManagement>
    <build> 
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <target>1.6</target>
                    <source>1.6</source>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>attach-sources</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <modules>
    .
    .
    .
    </modules>
    <repositories>
    .
    .
    .
    </repositories>
    <distributionManagement>
    .
    .
    .
    </distributionManagement>
    <pluginRepositories>
    .
    .
    .
    </pluginRepositories>
    <organization>
        <name>My</name>
    </organization>
    <profiles>
        <profile>
            <id>dist</id>
            <modules>
                <module>../my-assembly/my-runner</module>
            </modules>
        </profile>
    </profiles>
</project>

出口pom:

<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>
        <groupId>org.mytest</groupId>
        <artifactId>my-parent</artifactId>
        <version>6.0.00-SNAPSHOT</version>
        <relativePath>../../my-parent</relativePath>
    </parent>
    <artifactId>my-runner</artifactId>
    <dependencies>
        .
        .
        .
    </dependencies>

    <packaging>jar</packaging>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>single</goal>
                        </goals>
                        <id>create-runner</id>
                        <phase>package</phase>
                        <configuration>
                            <finalName>runner</finalName>
                                <appendAssemblyId>false</appendAssemblyId>
                            <descriptors>
                                    <descriptor>src/main/assembly/runner.xml</descriptor>
                            </descriptors>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

转轮文件(由导出pom引用):

<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>tar.gz</format>
        <format>zip</format>
        <format>dir</format>
    </formats>
    <id>runner</id>
    <dependencySets>
        .
        .
        .
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <outputFileNameMapping>${artifact.artifactId}.${artifact.extension}    </outputFileNameMapping>
            <includes>
                <include>org.mytest:*:jar</include>
                <include>org.mytest.systemobjects:*:jar</include>
            </includes>
            <excludes>
                <exclude>*:my-services-so:*</exclude>
                <exclude>*:my-services-tests:*</exclude>
                <exclude>*:my-runner:*</exclude>
            </excludes>
        </dependencySet>
    </dependencySets>
    <fileSets>
    .
    .
    .
    </fileSets>
</assembly>

提前谢谢你!

2 个答案:

答案 0 :(得分:0)

MNG-1994看,似乎在每个阶段,孩子的插件将在父母的插件之前执行。

您的父POM在包阶段指定源插件(生成源JAR)(如果未在执行中明确指定,这是源插件的默认阶段),但是生成tar.gz的程序集插件, zip和未压缩的项目完成版本,在源插件之前运行,因为程序集插件在子项中声明,源插件在父项中声明。

解决方案可以是更改源插件运行的阶段,以确保它在程序集插件之前运行,正好在package阶段(如prepare-package之前)才能运行。 (生命周期阶段的完整列表是here

在父级中,明确将阶段设置为prepare-package将使其成为:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>

这应该强制源插件在程序集插件之前执行,因此程序集插件现在应该拾取两个附加的JAR。

答案 1 :(得分:0)

使用classifier include添加另一个sources元素:

<include>org.mytest:*:jar:sources</include>

请参阅文档:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_dependencySet