简体   繁体   中英

How to build a Maven POM file for both Android and Java releases

My requirements

I have a Java ulitities library. I want to make 2 jar files. One for Android and the another for Java.

For Android jar, I want to exclude JDBC package.

I want to upload both jar files & theirs javadoc, javasources into the Maven remote repository.

so I can use the library as below

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

OR Android classifier

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

Here is my 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. Run mvn clean deploy for deploy Java profile

    Successfully . Maven deploy the following files into the remote repository:

    • mylib-1.0.jar
    • mylib-1.0.pom
    • mylib-1.0-javadoc.jar
    • mylib-1.0-sources.jar
    • And all their sign files
  2. Run mvn clean deploy -Dandroid for deploy Android profile

    The following files are generated:

    • mylib-1.0-android.jar // append classifier
    • mylib-1.0.pom
    • mylib-1.0-javadoc.jar
    • mylib-1.0-sources.jar
    • And all their sign files

I got error

    [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'

Question : why is this error happening?

The same issue has already been reported and it's mainly due to the fact that the main artifact uses a classifier (the android one) while sources and javadoc artifact don't, creating a misalignment.

Indeed, the error message is exactly pointing at issues on sources and javadoc artifacts:

[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'

Saying that the sources and the javadoc artifact couldn't be found (according to the main artifact classifier): a mismatch.

Luckily, you can fix this issue configuring the maven-source-plugin via its classifier option (default to sources ) and the maven-javadoc-plugin via its classifier option (default to javadoc ).

The android profile could actually simply specify the following property entries:

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

Indeed using the user properties specified by the aforementioned plugin goals for these options.

To make it even more robust, better to apply the following approach:

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

Then use the ${android.classifier} property for the classifier value of the maven-jar-plugin also: that is, centralising its value you ensure an easier maintenance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM