简体   繁体   English

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

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

My requirements我的要求

I have a Java ulitities library.我有一个 Java 实用程序库。 I want to make 2 jar files.我想制作 2 个 jar 文件。 One for Android and the another for Java.一个用于 Android,另一个用于 Java。

For Android jar, I want to exclude JDBC package.对于 Android jar,我想排除 JDBC 包。

I want to upload both jar files & theirs javadoc, javasources into the Maven remote repository.我想将 jar 文件和他们的 javadoc、javasources 上传到 Maven 远程存储库中。

so I can use the library as below所以我可以使用下面的库

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

OR Android classifier或 Android 分类器

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

Here is my POM.xml这是我的 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运行mvn clean deploy以部署 Java 配置文件

    Successfully .成功 Maven deploy the following files into the remote repository: Maven 将以下文件部署到远程存储库中:

    • mylib-1.0.jar mylib-1.0.jar
    • mylib-1.0.pom mylib-1.0.pom
    • mylib-1.0-javadoc.jar mylib-1.0-javadoc.jar
    • mylib-1.0-sources.jar mylib-1.0-sources.jar
    • And all their sign files以及他们所有的签名文件
  2. Run mvn clean deploy -Dandroid for deploy Android profile运行mvn clean deploy -Dandroid以部署 Android 配置文件

    The following files are generated:生成以下文件:

    • mylib-1.0-android.jar // append classifier mylib-1.0-android.jar // 附加分类器
    • mylib-1.0.pom mylib-1.0.pom
    • mylib-1.0-javadoc.jar mylib-1.0-javadoc.jar
    • mylib-1.0-sources.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.已经报告了相同的问题,这主要是由于主要工件使用分类器( android分类器)而源和 javadoc 工件不使用这一事实,从而造成错位。

Indeed, the error message is exactly pointing at issues on sources and javadoc artifacts:事实上,错误消息正是指向源代码和 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'

Saying that the sources and the javadoc artifact couldn't be found (according to the main artifact classifier): a mismatch.说找不到sourcesjavadoc工件(根据主要工件分类器):不匹配。

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 ).幸运的是,您可以通过其classifier选项(默认为sources )配置maven-source-plugin和通过其classifier选项(默认为javadoc )配置maven-javadoc-plugin来解决此问题。

The android profile could actually simply specify the following property entries: android配置文件实际上可以简单地指定以下property条目:

<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.然后使用${android.classifier}属性作为maven-jar-pluginclassifier值:也就是说,集中它的值可以确保更容易维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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