【发布时间】: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 文件之前如何执行此任务?
请帮帮我。 谢谢。
--编辑
更改阶段以初始化<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]
【问题讨论】:
-
我建议您查看multienv-maven-plugin,当然,这是您的单元/集成测试运行的默认值?