【问题标题】:Executing configuration tasks before creation of the jar在创建 jar 之前执行配置任务
【发布时间】:2017-01-25 15:30:54
【问题描述】:

我在一个 maven 项目中创建了两个配置文件 dev & prod,使用它可以将配置文件放置在配置路径中。但是这个文件复制过程是在 target/ 目录中创建 jar 文件之后发生的。

下面是我的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>
    <groupId>com.analytics</groupId>
    <artifactId>offline-process</artifactId>
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>Using DEV Profile</echo>
                                        <echo>Configuration File : src/main/resources/config/dev.conf</echo>
                                        <copy file="src/main/resources/config/dev.conf" tofile="src/main/resources/config/default.conf" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <phase>install</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>Using PROD Profile</echo>
                                        <echo>Configuration File : src/main/resources/config/prod.conf</echo>
                                        <copy file="src/main/resources/config/prod.conf" tofile="src/main/resources/config/default.conf" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    <version>1</version>
    <dependencies>
        ...
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.analytics.MainClass</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>

    </build>
</project>

mvn clean install 输出的尾部:

...
[INFO] META-INF/maven/ already added, skipping
[INFO] META-INF/maven/javax.mail/ already added, skipping
[INFO] 
[INFO] --- maven-install-plugin:2.3:install (default-install) @ offline-process ---
[INFO] Installing /home/pallav/offline_process_master/analytics/target/offline-process-1.jar to /home/pallav/.m2/repository/com/analytics/offline-process/1/offline-process-1.jar
[INFO] Installing /home/pallav/offline_process_master/analytics/pom.xml to /home/pallav/.m2/repository/com/analytics/offline-process/1/offline-process-1.pom
[INFO] Installing /home/pallav/offline_process_master/analytics/target/offline-process-1-jar-with-dependencies.jar to /home/pallav/.m2/repository/com/analytics/offline-process/1/offline-process-1-jar-with-dependencies.jar
[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (default) @ offline-process ---
[INFO] Executing tasks
     [echo] Using DEV Profile
     [echo] Configuration File : src/main/resources/config/dev.conf
     [copy] Copying 1 file to /home/pallav/offline_process_master/analytics/src/main/resources/config
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.309s
[INFO] Finished at: Wed Jan 25 20:42:09 IST 2017
[INFO] Final Memory: 41M/339M

在创建 jar 文件之前如何执行此任务?

请帮帮我。 谢谢。

--编辑 更改阶段以初始化&lt;echo&gt; 后,消息像之前一样出现,但复制任务没有发生。

[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building offline-process 1
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ offline-process ---
[INFO] Deleting /home/pallav/offline_process_master/analytics/target
[INFO] 
[INFO] --- maven-antrun-plugin:1.1:run (default) @ offline-process ---
[INFO] Executing tasks
     [echo] Using DEV Profile
     [echo] Configuration File : src/main/resources/config/dev.conf
[INFO] Executed tasks
[INFO] 

【问题讨论】:

  • 我建议您查看multienv-maven-plugin,当然,这是您的单元/集成测试运行的默认值?

标签: java maven pom.xml


【解决方案1】:

当您必须使用 Maven 做一些不寻常的事情时,这通常是设计不佳的标志。

考虑使用包含要读取的配置文件名称的系统属性。这将允许您在没有任何 Maven 魔法的情况下在任何地方使用相同的 JAR。

依赖于 JAR 的代码可以配置系统属性来选择要加载的配置。单元测试可以将其设置为DevTestProd 将是默认设置,以确保在您无法定义属性时(例如,在 Web 容器中)代码可以正常工作。

【讨论】:

    【解决方案2】:

    我猜你必须改变你的执行阶段。 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

    在标签“profiles>profile>build>plugins>plugin>executions>execution>phase”中将“install”替换为“initialize”。 像这样:

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>Using DEV Profile</echo>
                                        <echo>Configuration File : src/main/resources/config/dev.conf</echo>
                                        <copy file="src/main/resources/config/dev.conf" tofile="src/main/resources/config/default.conf" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>prod</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.1</version>
                        <executions>
                            <execution>
                                <phase>initialize</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <tasks>
                                        <echo>Using PROD Profile</echo>
                                        <echo>Configuration File : src/main/resources/config/prod.conf</echo>
                                        <copy file="src/main/resources/config/prod.conf" tofile="src/main/resources/config/default.conf" />
                                    </tasks>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    

    我在两个配置文件中都替换了:

    <phase>install</phase> 
    

    <phase>initialize</phase>
    

    用这个运行你的 mvn 命令:

    mvn initialize install
    

    mvn clean initialize install -DskipTests=true
    

    【讨论】:

    • 谢谢,我已经更新了问题描述。更改 phase 后,此时间副本不起作用
    猜你喜欢
    • 2020-02-04
    • 2013-09-03
    • 2015-02-02
    • 2023-03-21
    • 2019-02-22
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多