java - 如何切换maven项目?

标签 java maven

让我从例子开始。假设有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 依赖于所有这些实现项目。我想创建一个 CustomerFactoryIntefaceProjectCustomerFactoryIntefaceProject 将依赖于 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/

相关文章:

java - JUnit 5 的 Travis CI 构建失败

java - GWT Horizo​​ntalPanel setSpacing?

AWS EMR 的 Java 客户端,listSteps 不显示最新步骤

java - 无法登录。Google Play 游戏 Libgdx

java - JUnit 测试 : Why is Maven (Surefire) so much slower than running on Eclipse?

java - 如何在不启动新的 jvm 进程的情况下从 ant 调用 maven?

java - JTable 和 JScrollPane 问题

java - 不同尺寸的 Swing 面板

java - 无法加载 JDBC 驱动程序类 - Spring Batch 独立程序

maven - 充满 "... and modules"和重复项的 IntelliJ 项目结构