如何通过 Maven 配置文件选择根上下文

How to choose root-context by maven profile

我正在使用 maven 打包一个 ear 文件。我有 2 个配置文件。 1 个配置文件 (aras) 用于生产。另一个配置文件 (aras_nb) 用于夜间构建。我想打包 2 个不同的耳文件。两个 ear 文件应该 运行 在同一个 weblogic 上。 ear-Files 也有不同的数据源。那已经有效了。 现在我想通过 maven 配置文件选择 URL。 我需要做什么才能得到这个 运行ning?

我有一个主项目、一个 ear-project、一个 web-project 和一个 ejb-project。

主项目的pom:

<groupId>xxx.yyy</groupId>
<artifactId>Aras</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>Aras</name>

<properties>
    <application.name>Aras</application.name>
    <application.ear.name>Aras-ear</application.ear.name>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <context-root>value</context-root>
</properties>

<modules>
    <module>Aras-ear</module>
    <module>Aras-web</module>
    <module>Aras-ejb</module>
</modules>

<profiles>
    <profile>
        <id>choose_environment</id>
        <build>
            <!-- enable resource filter to set the datasource name -->
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <filtering>true</filtering>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>aras</id>
        <properties>
            <datasource.name>ArasDataSource</datasource.name>
            <environment.name>ARAS</environment.name>
            <contextroot.name>/aras</contextroot.name>
        </properties>
    </profile>
    <profile>
        <id>aras_nb</id>
        <properties>
            <datasource.name>ArasNbDataSource</datasource.name>
            <environment.name>ARAS-NB</environment.name>
            <contextroot.name>/aras_nb</contextroot.name>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

在 eclipse 中,我使用配置文件 "choose_environment, aras" 执行 maven "clean package"。 当我更新项目时,上下文根自动设置回 "Aras-web".

找到了。

aras 的 pom:

<profiles>
    <profile>
        <id>aras</id>
        <properties>
            <datasource.name>ArasDataSource</datasource.name>
            <environment.name>ARAS</environment.name>
            <rootcontext.name>aras</rootcontext.name>
            <earfile.name>Aras-ear</earfile.name>
        </properties>
    </profile>
    <profile>
        <id>aras_nb</id>
        <properties>
            <datasource.name>ArasNbDataSource</datasource.name>
            <environment.name>ARAS-NB</environment.name>
            <rootcontext.name>aras_nb</rootcontext.name>
            <earfile.name>Aras_nb-ear</earfile.name>
        </properties>
    </profile>
</profiles>

aras-ear 的 pom:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-ear-plugin</artifactId>
            <version>2.8</version>
            <configuration>
                <version>6</version>
                <defaultLibBundleDir>lib</defaultLibBundleDir>
                <finalName>${earfile.name}-${project.Version}</finalName>
                <modules> 
                    <webModule>
                        <groupId>${project.groupId}</groupId>
                        <artifactId>Aras-web</artifactId>
                        <contextRoot>/${rootcontext.name}</contextRoot>
                    </webModule>
                </modules>
            </configuration>
        </plugin>
    </plugins>
</build>