如何为Android和Java版本构建Maven POM文件

时间:2016-09-16 16:40:32

标签: java maven-3 pom.xml maven-profiles

我的要求

我有一个Java ulitities库。我想制作2个jar文件。一个用于Android,另一个用于Java。

对于Android jar,我想排除JDBC包。

我想上传两个jar文件&他们的javadoc,javasources进入Maven远程存储库。

所以我可以使用下面的库

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mylib</artifactId>
    <version>1.0</version>
</dependency>

OR Android分类器

<dependency>
    <groupId>com.mycompany</groupId>
    <artifactId>mylib</artifactId>
    <version>1.0</version>
    <classifier>android</classifier>
</dependency>

这是我的POM.xml

<project>
    <!-- .... -->

    <groupId>com.mycompany</groupId>
    <artifactId>mylib</artifactId>
    <version>1.0</version>

    <name>mylib</name>
    <packaging>jar</packaging>

    <!-- .... -->

    <build>
        <plugins>
            <!-- compiler -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <!-- .... -->
            </plugin>

            <!-- jar -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <!-- .... -->
            </plugin>

            <!-- source -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-source-plugin</artifactId>
                <!-- .... -->
            </plugin>

            <!-- javadoc -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <!-- .... -->
            </plugin>

            <!-- nexus-staging-maven -->
            <plugin>
                <groupId>org.sonatype.plugins</groupId>
                <artifactId>nexus-staging-maven-plugin</artifactId>
                <!-- .... -->
            </plugin>

            <!-- maven-gpg -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-gpg-plugin</artifactId>
                <!-- .... -->
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- All files - Java -->
        <profile>
            <id>java</id>
            <activation>
                <property>
                    <name>!android</name>
                </property>
            </activation>
        </profile>

        <!-- Android - Excluding JDBC, JPA packages -->
        <profile>
            <id>android</id>
            <activation>
                <property>
                    <name>android</name>
                </property>
            </activation>
            <build>
                <plugins>

                    <!-- Exclude some packages -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-compiler-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/jdbc/**</exclude>
                            </excludes>
                        </configuration>
                    </plugin>

                    <!-- Exclude some packages -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <classifier>android</classifier>
                            <excludes>
                                <exclude>**/jdbc/**</exclude>
                            </excludes>
                        </configuration>
                    </plugin>

                    <!-- Exclude some packages --> 
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-source-plugin</artifactId>
                        <configuration>
                            <excludes>
                                <exclude>**/jdbc/**</exclude>
                            </excludes>
                        </configuration>
                    </plugin>

                    <!-- Exclude some packages -->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-javadoc-plugin</artifactId>
                        <configuration>
                            <sourcepath>${project.basedir}/src/main/java</sourcepath>
                            <sourceFileExcludes>
                                <sourceFileExclude>**/jdbc/**</sourceFileExclude>
                            </sourceFileExcludes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

    <dependencies>
        <!-- .... -->
    </dependencies>

</project>

Maven CLI

  1. 运行 mvn clean deploy 以部署Java配置文件

    成功即可。 Maven将以下文件部署到远程存储库中:

    • MYLIB-1.0.jar
    • MYLIB-1.0.pom
    • MYLIB-1.0-javadoc.jar
    • MYLIB-1.0-sources.jar
    • 及其所有签名文件
  2. 运行 mvn clean deploy -Dandroid 以部署Android配置文件

    生成以下文件:

    • mylib-1.0-android.jar //追加分类器
    • MYLIB-1.0.pom
    • MYLIB-1.0-javadoc.jar
    • MYLIB-1.0-sources.jar
    • 及其所有签名文件
  3. 我收到了错误

        [ERROR] Repository "mycompany-1082" failures
        [ERROR]   Rule "sources-staging" failures
        [ERROR]     * Missing: no main jar artifact found in folder '/com/mycompany/mylib/1.0'
        [ERROR]   Rule "javadoc-staging" failures
        [ERROR]     * Missing: no main jar artifact found in folder '/com/mycompany/mylib/1.0'
    

    问题:为什么会发生此错误?

1 个答案:

答案 0 :(得分:1)

same issue已被报道,主要原因是主要工件使用了分类器(android),而源和javadoc工件不是,造成错位。

实际上,错误消息正是指向源和javadoc工件上的问题:

[ERROR]   Rule "sources-staging" failures
[ERROR]     * Missing: no main jar artifact found in folder '/com/mycompany/mylib/1.0'
[ERROR]   Rule "javadoc-staging" failures
[ERROR]     * Missing: no main jar artifact found in folder '/com/mycompany/mylib/1.0'

说无法找到sourcesjavadoc工件(根据主工件分类器):不匹配。

幸运的是,您可以解决此问题,通过classifier选项(默认为maven-source-plugin)配置sourcesmaven-javadoc-plugin通过classifier选项配置javadoc默认为android)。

property个人资料实际上只能指定以下<properties> <maven.source.classifier>android-sources</maven.source.classifier> <maven.javadoc.classifier>android-javadoc</maven.javadoc.classifier> </properties> 条目:

<properties>
   <android.classifier>android</android.classifier>
   <maven.source.classifier>${android.classifier}-sources</maven.source.classifier>
   <maven.javadoc.classifier>${android.classifier}-javadoc</maven.javadoc.classifier>
</properties>

确实使用上述插件目标为这些选项指定的用户属性。

为了使其更加健壮,最好采用以下方法:

${android.classifier}

然后将classifier属性用于maven-jar-plugin的{​​{1}}值:也就是说,集中其值可确保更轻松的维护。