我有一个带有子模块的项目 a:jar
它需要一组不同的依赖项,具体取决于编译它的平台。所有平台的代码都是相同的。例如。在 Android 上,httpcomponents 库已经与操作系统捆绑在一起,而我必须将它包含在构建 J2SE 环境中。我还有另一个子模块,它将几个子模块及其依赖项组合成一个存档。如何可靠地配置程序集子模块,以获取为相应平台编译的所有子模块,以及适合该平台的依赖项?
我尝试使用配置文件创建 a:jar:android
和 a:jar:j2se
.但是,声明对其中之一的依赖会导致程序集中出现奇怪的依赖关系。即 dependency:tree
的装配项目有时会包含对 a:jar:j2se
的依赖项。 (不管我是否声明在程序集中使用 a:jar:android
或 a:jar:j2se
),有时是另一个。在我更新了 a
的 jar 之后,它(经常)发生了变化在本地存储库中。在装配项目中切换也使用配置文件。
我可以解决 by applying the same dependencies to the assembly project's profiles根据各个子模块配置文件的需要。但由于我必须在 POM 中重复自己,可能有一种更专业的方法来实现这一点。由于我对 Maven 很陌生,我想知道它是什么?我不想复制代码(由于代码保持不变,这甚至会重复更多)并且我不喜欢复制 POM 的某些部分,因为由于版本升级而更改它们可能会变得复杂。
一些具体的 Material :来自 a:jar
的依赖项的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 组装插件)配置文件:
<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>
我想摆脱标记的依赖声明。
最佳答案
有几种解决方案:
<exclusions>
对于不必要的依赖。 关于maven - 以 Maven 方式为不同平台构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7650727/