本文介绍了以 Maven 方式为不同平台构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有子模块 a:jar 的项目,它需要一组不同的依赖项,具体取决于它是为哪个平台编译的.所有平台的代码都相同.例如.在 Android 上,httpcomponents 库已经与操作系统捆绑在一起,而我必须将它包含在 J2SE 环境的构建中.我还有另一个子模块,它将几个子模块及其依赖项组装到一个存档中.我如何可靠地配置程序集子模块,以获取为相应平台编译的所有子模块及其适用于该平台的依赖项?

I have a project with a submodule a:jar which needs a different set of dependencies, depending for which platform it is compiled. The code is the same for all platforms. E.g. on Android the httpcomponents library is already bundled with the OS, whereas I have to include it for builds for J2SE environments. I also have another submodule, which assembles several submodules and their dependencies into an archive. How can I reliably configure the assembly submodule, to take all the submodules compiled for the respective platform, and their dependencies appropriate for that platform?

我尝试使用配置文件来创建 a:jar:androida:jar:j2se.但是,声明对其中之一的依赖会导致程序集中出现奇怪的依赖关系.即,汇编项目的 dependency:tree 有时会包含对 a:jar:j2se 的依赖(不管我是否声明使用 a:jar:androida:jar:j2se 在程序集中),有时是另一个.在我更新本地存储库中 a 的 jar 之后,它(经常)发生了变化.在装配项目中切换也使用配置文件.

I tried using profiles to create a:jar:android and a:jar:j2se. However, declaring a dependency on one of these would result in strange dependencies in the assembly. I.e., the dependency:tree of the assembly project would sometimes include the dependencies on a:jar:j2se (doesn't matter whether I declared to use the a:jar:android or a:jar:j2se in the assembly), and sometimes the other. It changed (often) after I updated the jars of a in the local repository. Switching in the assembly project uses also profiles.

我可以通过应用相同的依赖项来解决到组装项目的配置文件,根据各个子模块配置文件的需要.但是由于我必须在 POM 中重复我自己,因此可能有一种更专业的方法来实现这一点.由于我对 maven 很陌生,我想知道它是什么?我不想复制代码(因为代码保持不变,这甚至会更重复)并且我不喜欢复制 POM 的部分,因为由于版本升级而更改它们可能会变得复杂.

I could solve that by applying the same dependencies to the assembly project's profiles as are needed by the individual submodules profile. But as I have to repeat myself in the POMs, there is probably a more maven-y way to achieve that. As I am quite new to maven, I wonder what it is? I don't want to duplicate the code (that would even be more repeating as the code stays the same) and I don't like to duplicate parts of the POM as changing them due to version upgrades can get complicated.

一些具体的材料:来自 a:jar 的 POM 的依赖:

Some concrete material: Dependencies from a:jar's POM:

  <dependencies>
    .....
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.0.1</version>
      <scope>compile</scope>
      <!-- Exclusion in the common part, they are provided in the profiles from different sources -->
      <exclusions>
        <exclusion>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
        </exclusion>
        ....
      </exclusions>
    </dependency>
  </dependencies>
  <profiles>
    <profile>
      <id>android</id>
      <dependencies>
        <dependency>
          <groupId>com.google.android</groupId>
          <artifactId>android</artifactId>
          <version>1.6_r2</version>
          <scope>provided</scope>
        </dependency>
      </dependencies>
    </profile>
    <profile>
      <id>java</id>
      <dependencies>
        <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.0.1</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.3</version>
          <scope>compile</scope>
        </dependency>
      </dependencies>
    </profile>
  </profiles>

组装项目(使用 Maven 组装插件)配置文件:

The assembly project (which uses the Maven Assembly Plugin) profiles:

  <profiles>
    <profile>
      <id>android</id>
      <dependencies>
        <dependency>
          <groupId>a</groupId>
          <artifactId>a</artifactId>
          <version>${project.version}</version>
          <classifier>android</classifier>
          <type>jar</type>
        </dependency>
        <!-- Duplicate -->
        <dependency>
          <groupId>com.google.android</groupId>
          <artifactId>android</artifactId>
          <version>1.6_r2</version>
          <scope>provided</scope>
        </dependency>
        <!-- Duplicate -->
      </dependencies>
    </profile>
    <profile>
      <id>java</id>
      <dependencies>
        <!-- Duplicate -->
        <dependency>
          <groupId>org.apache.httpcomponents</groupId>
          <artifactId>httpclient</artifactId>
          <version>4.0.1</version>
          <scope>compile</scope>
        </dependency>
        <dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.3</version>
          <scope>compile</scope>
        </dependency>
        <!-- /Duplicate -->
        <dependency>
          <groupId>a</groupId>
          <artifactId>a</artifactId>
          <version>${project.version}</version>
          <classifier>java</classifier>
         <type>jar</type>
        </dependency>
      </dependencies>
    </profile>
  </profiles>

我想去掉标记的依赖声明.

I'd like to get rid of the marked dependency declarations.

推荐答案

有几种解决方案:

  1. 这些子模块的依赖项可以声明为提供",在这种情况下,在项目中,您包括对子模块的依赖项以及平台没有的显式依赖项.

  1. These submodule's dependencies could be declared "provided", in this case in a project you include the dependency to the submodule along with explicit dependencies which a platform doesn't have.

对不必要的依赖项使用 .

Use <exclusions> for unnecessary dependencies.

使用上面的 (1) 或 (2) 创建另一个结构化"子模块 a-android:pom a-j2se:pom,它只描述依赖项,并从您的项目中使用这些模块.

Using (1) or (2) above create yet another "structural" submodules a-android:pom a-j2se:pom which just describe dependencies, and use these modules from your projects.

这篇关于以 Maven 方式为不同平台构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-18 11:26