1

我想在 pom 中使用 Maven 进行构建,可以使用两个不同的配置文件构建。

换句话说,我的任务是强制构建,以便构建在目标文件夹中生成两个不同的构建,就像我将执行“maven install -P”两次一样。

所以,要更清楚:

  • 一次调用
  • 基于当时关联的配置文件的两个不同结果

我已经尝试过,exec-maven-plugin但我不确定是否是正确的方法,因为它没有做我想要的。

我该怎么做?

这是我正在尝试的pom。任务是:

  • 从另一个项目复制资源
  • 根据配置文件替换内容
  • 构建两个配置文件。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>MavenDoubleInstallation</artifactId>

    <profiles>
        <profile>
            <id>profileA</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>profileA</env>
                <token>/path/for/profileA</token>
            </properties>
        </profile>
        <profile>
            <id>profileB</id>
            <properties>
                <env>profileB</env>
                <token>/path/for/profileB</token>
            </properties>
        </profile>
    </profiles>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <includeEmptyDirs>true</includeEmptyDirs>
                </configuration>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/target/classes/${env}/result</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.basedir}/../SourceProject/resources/result</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.3</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <basedir>${basedir}/target/classes/${env}/result</basedir>
                    <includes>
                        <include>**/*.sh</include>
                    </includes>
                    <replacements>
                        <replacement>
                            <token>%%TOKEN%%</token>
                            <value>${token}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <id>build-profileA</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>maven</executable>
                            <arguments>
                                <argument>-P profileA -X</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>build-profileB</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>maven</executable>
                            <arguments>
                                <argument>-P profileB -X</argument>
                            </arguments>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>
4

0 回答 0