在创建jar之前执行配置任务

时间:2017-01-25 15:30:54

标签: java maven pom.xml

我在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文件之前执行此任务?

请帮助我。 感谢。

- 编辑 更改阶段后初始化<echo>消息就像之前一样,但复制任务没有发生。

[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] 

2 个答案:

答案 0 :(得分:0)

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

在标签“profiles&gt;个人资料&gt;构建&gt;插件&gt;插件&gt;执行&gt;执行&gt;阶段”中将“installize”替换为“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

答案 1 :(得分:0)

当你不得不与Maven做一些不寻常的事情时,通常是设计糟糕的迹象。

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

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