0

我有一个 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>

先感谢您!

4

2 回答 2

0

使用分类器添加另一个include元素: sources

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

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

于 2013-10-01T09:15:52.493 回答
0

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

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

package一个解决方案可以是更改源插件运行的阶段,以确保它在程序集插件之前运行,在该阶段之前的某些东西prepare-package可以工作。(生命周期阶段的完整列表在这里

在父级中,将阶段显式设置为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。

于 2013-09-30T12:20:53.307 回答