简体   繁体   中英

Executing configuration tasks before creation of the jar

I created two profiles, dev & prod , in a maven project using which the configuration file could be placed in the config path. But this process of copying of files takes place after the jar file has been created in the target/ directory.

Below is my 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>

tail of mvn clean install output :

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

How to do this task before creation of the jar file takes place?

Kindly help me out. Thanks.

--Edit After changing the phase to initialize <echo> messages appeared like earlier but copy task did not happen.

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

I guess you have to change your execution phase. https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference

Replace "install" by "initialize" in your tag "profiles>profile>build>plugins>plugin>executions>execution>phase". Like this :

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

I replaced, in both profiles :

<phase>install</phase> 

by

<phase>initialize</phase>

Run your mvn command with this :

mvn initialize install

or

mvn clean initialize install -DskipTests=true

When you have to do unusual things with Maven, it's often a sign of bad design.

Consider using a System property which contains the name of the config file to read. That would allow you to use the same JAR everywhere without any Maven magic.

Code which depends on the JAR could configure the System property to select which config to load. Unit tests could set it to Dev or Test , Prod would be the default to make sure the code works when you can't define the property (for example, in web containers).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM