Maven个人资料:maven-antrun-plugin& maven-war-plugin交互

时间:2013-02-05 14:42:07

标签: maven maven-profiles maven-antrun-plugin

我有一个Spring framework项目,其中包含src/main/webapp/WEB-INF中的以下三个文件:

project.propterties
project-servlet.xml
project-security.xml

我正在尝试使用maven profiles如下所示在prod版本的情况下“注入”上述文件:

        <profile>
        <id>prod-build</id>
        <build>
            <plugins>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <delete
                                        file="${project.build.directory}/${project.build.finalName}/WEB-INF/project.properties" />
                                    <copy file="src/main/config/prod/project.properties"
                                        tofile="${project.build.directory}/${project.build.finalName}/WEB-INF/project.properties" />
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.3</version>
                </plugin>
            </plugins>
            <resources>
                <resource>
                    <directory>src/main/resources/prod</directory>
                </resource>
            </resources>
        </build>
    </profile>

问题是maven-antrun-plugin有机会运行,war已经打包。蚂蚁<target>会运行并进行适当的更改,但为时已晚。

命令行:

$ mvn -Pprod-build clean install

mvn输出:

    [INFO] --- maven-antrun-plugin:1.7:run (default) @ FocusMVN ---
[INFO] Executing tasks

main:
     [copy] Copying 1 file to J:\work\workspace\FocusMVN\target\myproject\WEB-INF
[INFO] Executed tasks
[INFO] 
[INFO] --- maven-war-plugin:2.3:war (default-war) @ FocusMVN ---
[INFO] Packaging webapp
[INFO] Assembling webapp [FocusMVN] in [J:\work\workspace\FocusMVN\target\myproject]
[INFO] Processing war project
[INFO] Copying webapp resources [J:\work\workspace\FocusMVN\src\main\webapp]
[INFO] Webapp assembled in [3198 msecs]
[INFO] Building war: J:\work\workspace\FocusMVN\target\myproject.war
[INFO] 
[INFO] --- maven-install-plugin:2.3.1:install (default-install) @ FocusMVN ---
[INFO] Installing J:\work\workspace\FocusMVN\target\myproject.war to C:\Users\mansoork\.m2\repository\ca\utoronto\med\dc\FocusMVN\0.0.1-SNAPSHOT\FocusMVN-0.0.1-SNAPSHOT.war
[INFO] Installing J:\work\workspace\FocusMVN\pom.xml to C:\Users\mansoork\.m2\repository\ca\utoronto\med\dc\FocusMVN\0.0.1-SNAPSHOT\FocusMVN-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

任何指针都将非常感激。

2 个答案:

答案 0 :(得分:1)

阶段中使用 prepare-package 而不是 package

而不是 maven-antrun-plugin 尝试使用 copy-maven-plugin

<plugin>
            <groupId>com.github.goldin</groupId>
            <artifactId>copy-maven-plugin</artifactId>
            <version>0.2.5</version>
            <executions>
                <execution>
                    <id>create-archive</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                    <configuration>
                        <resources>
                            <resource>
                            <targetPath>${basedir}/target/</targetPath>
                            <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                            <includes>
                                <include>**/project*</include>
                            </includes>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>

答案 1 :(得分:1)

你注意到这一行

[INFO] Copying webapp resources [J:\work\workspace\FocusMVN\src\main\webapp]

发生了什么事?

maven-war-pluginsrc/main/webapp(其中包含 dev project.properties)的内容复制到战争中。在执行此操作时,它将替换刚刚由ant任务复制的那个。

您可以尝试从maven-war-plugin复制的文件集中排除project.properties(因为它已经被您的ant任务复制了。)

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>     
    <configuration>
       <warSourceExcludes>WEB-INF/project.propterties</warSourceExcludes>
    </configuration>
</plugin>

reference