0

我有一个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] ------------------------------------------------------------------------

任何指针将不胜感激。

4

2 回答 2

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>
于 2013-02-05T14:48:43.673 回答
1

你注意到这条线了吗

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

这是怎么回事 ?

The maven-war-plugin copy the content of src/main/webapp (which contains your dev project.properties) to the war. And while doing this it replace the one just copied by the ant task.

What you can try is to exclude the project.properties from the fileset copied by the maven-war-plugin (since it was already copied by your ant task.)

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

reference

于 2013-02-05T16:05:07.490 回答