让我从例子开始。假设有Factory接口(interface)和Customer类:
public interface CustomerFactory(){
Customer create();
}
public class Customer(){
private String name;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
}
假设我有 CustmerProject maven 项目需要 CustomerFactory。我想创建几个具有 CustomerFactory 实现的 Maven 项目(每个项目一个实现),但我不希望 CustmerProject 依赖于所有这些实现项目。我想创建一个 CustomerFactoryIntefaceProject,CustomerFactoryIntefaceProject 将依赖于 CustomerFactoryImplementationProject,并将其中一个 CustomerFactoryImplementationProject 作为 CustomerFactoryIntefaceProject。我知道这是可能的,但在实践中我该如何做到这一点?
P.S.如果问题不清楚请问我。
最佳答案
您可以参数化 CustomerFactoryImplementationProject 的 artifactId 或版本,并使用 Maven 配置文件在构建过程中选择实现。
假设您有以下 Artifact :
- 客户
- 客户工厂-1
- 客户工厂-2
因此,在客户项目的 pom.xml 中,您将需要:
<dependency>
<groupId>base</groupId>
<artifactId>${customer.factory.artifact}</artifact>
<version>123</version>
</dependency>
....
<profiles>
<profile>
<id>customerFactory1</id>
<properties>
<customer.factory.artifact>customer-factory-1</customer.factory.artifact>
</properties>
</profile>
<profile>
<id>customerFactory2</id>
<properties>
<customer.factory.artifact>customer-factory-2</customer.factory.artifact>
</properties>
</profile>
</profiles>
现在只需在构建过程中选择配置文件,如下所示:
mvn -P customerFactory2 install
<小时/>
...或者实际上您可以直接分析依赖项 block :
<profiles>
<profile>
<id>customerFactory1</id>
<dependencies>
<dependency>
<groupId>base</groupId>
<artifactId>customer-factory-1</artifact>
<version>123</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>customerFactory2</id>
<dependencies>
<dependency>
<groupId>base</groupId>
<artifactId>customer-factory-2</artifact>
<version>123</version>
</dependency>
</dependencies>
</profile>
</profiles>
关于java - 如何切换maven项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16731651/