[war打包]在maven-antrun-plugin复制更改了配置文件但是maven-war-plugin打包出来配置文件没变化(草稿)

16 篇文章 0 订阅
14 篇文章 0 订阅

前言 &背景介绍

在某个java-web项目中,需要根据maven的profile的配置进行properties文件的切换,
例如,项目的配置文件结构如下:
这里写图片描述

在resources文件夹下面分别有profiles文件夹及conf文件夹【重点关注】,
profiles下面有三个环境,分别是dev,test,product,而conf的配置文件都是跟随开发人员选择的环境【dev,test,product】而改变的,假如选择的是product,那么构建以后conf下面的配置文件就会被product下面的四个文件替换掉。

重点说说env.properties的内容,作为替换成功与否的构建标准,注意,conf下面默认的配置文件是dev的,构建与否不会替换掉。
这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

好了,几个环境下面的env文件内容说明完毕。

下面将构建配置文件pom内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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>net.w2p</groupId>
    <artifactId>MicroWeb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <!--设置配置环境profile begin -->
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
    </profiles>
    <!--设置配置环境profile bend -->

    <!--好了,是不是经常发现pom文件一加一点东西idea的编译环境就会自动变化1.5版本的jdk??加上这个强行指定编译版本就没问题了。-->

    <build>
        <!--名字统一一下-->
        <finalName>${project.artifactId}</finalName>



        <plugins>




            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


            <!--打包替换-->


            <plugin>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <!--<phase>package</phase>-->

                        <phase>package</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>

                                <!--<echo message="自动处理,下一步要删除该路径:${project.build.directory}/${project.build.finalName}/WEB-INF/classes/conf" level="debug"/>-->
                                <delete dir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/conf"/>
                                <!--<copy todir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/conf">-->
                                <!--<fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/profiles/${package.environment}/"/>-->
                                <!--</copy>-->
                                <copy todir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/conf">
                                    <fileset dir="src/main/resources/profiles/${package.environment}"/>
                                </copy>

                                <delete dir="${project.build.directory}/${project.build.finalName}/classes/conf"/>
                                <!--<copy todir="${project.build.directory}/${project.build.finalName}/classes/conf">-->
                                    <!--<fileset dir="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/profiles/${package.environment}/"/>-->
                                <!--</copy>-->
                                <copy todir="${project.build.directory}/${project.build.finalName}/classes/conf">
                                    <fileset dir="src/main/resources/profiles/${package.environment}"/>
                                </copy>

                                <!-- <delete file="./src/main/webapp/js/config.js" />   -->
                                <!--<move file="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/profiles/${package.environment}/**" tofile="${project.build.directory}/${project.build.finalName}/WEB-INF/classes/conf/**"/>-->
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <!--打包文件替换end-->



            <!--war 打包 begin -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <!--
                      Exclude JCL and LOG4J since all logging should go through SLF4J.
                      Note that we're excluding log4j-<version>.jar but keeping
                      log4j-over-slf4j-<version>.jar
                    -->

                    <webXml>web\WEB-INF\web.xml</webXml>
                    <!--指定jsp、js、css的路劲  -->
                    <warSourceDirectory>web</warSourceDirectory>
                    <packagingExcludes>
                        WEB-INF/lib/commons-logging-*.jar,
                        %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
                    </packagingExcludes>

                    <webResources>


                    </webResources>
                </configuration>
            </plugin>
            <!--war 打包 end -->





        </plugins>
    </build>


    <dependencies>

        <!--基础类库-->
        <dependency>
            <!--Group   net.funfunle-->
            <!--Name    baselib-->
            <groupId>net.funfunle</groupId>
            <artifactId>baselib</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <!--注意,这玩意会同时引用slf4j-api以及slf4j-log4j12,这两个是会冲突的。其中一个不引用了。-->
            <exclusions>
                <!--去除不用的依赖包-->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--app项目对应api项目 begin -->
        <dependency>
            <groupId>net.w2p</groupId>
            <artifactId>MicroBaseApi</artifactId>
            <version>1.0-SNAPSHOT</version>
            <!--注意,这玩意会同时引用slf4j-api以及slf4j-log4j12,这两个是会冲突的。其中一个不引用了。-->
            <exclusions>
                <!--去除不用的依赖包-->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--app项目对应api项目 end-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>



        <!-- Servlet web -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>



        <!--七牛 begin -->

        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>[7.2.0, 7.2.99]</version>
        </dependency>
        <!--七牛 end -->

        <!--apache commons 类库 begin -->
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <!--apache commons 类库 end -->


    </dependencies>

</project>

好了,idea上面的界面如下:
这里写图片描述

接下来将分别打包test及product环境。

test环境打包

点击clean及package之后,构建内容如下:
这里写图片描述

这里写图片描述

env文件内容如下:
这里写图片描述

而war文件中,

这里写图片描述
env内容如下:

这里写图片描述

dev。。。。。。。

推测:maven是先进行war打包,然后antrun才进行替换的。所以替换无效。

相关资料参考

请看看链接:

Maven: antrun-plugin vs war-plugin

这里写图片描述

Maven-antrun-plugin modify artifact before war is packaged

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.6</version>
    <executions>
        <execution>
            <id>deploy-ui</id>
            <phase>package</phase>
            <inherited>false</inherited>
            <configuration>
                <target>
           <-- JAR was modified in here -->
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <executions>
        <execution>
            <!-- First step is to disable the default-war build step. -->
            <id>default-war</id>
            <phase>none</phase>
        </execution>
        <execution>
            <!-- Second step is to create an exploded war. Done in prepare-package -->
            <id>war-exploded</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>exploded</goal>
            </goals>
        </execution>
        <execution>
            <!-- Last step is to make sure that the war is built in the package phase -->
            <id>custom-war</id>
            <phase>package</phase>
            <goals>
                <goal>war</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Maven profiles: maven-antrun-plugin & maven-war-plugin interaction

Maven profiles: maven-antrun-plugin & maven-war-plugin interaction
Ask Question
up vote
0
down vote
favorite
I have a Spring framework project with the following three files in src/main/webapp/WEB-INF:

project.propterties
project-servlet.xml
project-security.xml
I am trying to use maven profiles as follows to 'inject' the above mentioned files in the case of a prod build:

        <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>
The problem is by the time maven-antrun-plugin gets a chance to run, the war has already been packaged. The ant <target> does run and makes appropriate changes, but too late.

The command line:

$ mvn -Pprod-build clean install
mvn output:

    [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] ------------------------------------------------------------------------
Any pointers will be greatly appreciated.

maven maven-profiles maven-antrun-plugin
shareimprove this question
edited Feb 5 '13 at 15:15
asked Feb 5 '13 at 14:42

kmansoor
1,47253567
1
You can bind antrun to prepare-package phase instead of package one. – Andrew Logvinov Feb 5 '13 at 14:45
@Andrew Logvinov Thanks; It doesn't appear to make a difference; I have updated my question with mvn output, can you please take a look? – kmansoor Feb 5 '13 at 15:16 
Which maven version do you use? – khmarbaise Feb 7 '13 at 12:30
@khmarbaise, I'm using Maven 3.0.4 – kmansoor Feb 7 '13 at 16:27
add a comment
2 Answers
active oldest votes
up vote
0
down vote
accepted
Did you note this line

[INFO] Copying webapp resources [J:\work\workspace\FocusMVN\src\main\webapp]
What's going on ?

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

shareimprove this answer
edited Feb 5 '13 at 17:18
answered Feb 5 '13 at 16:05

ben75
22.4k664110
You mean warSourceExcludes? – kmansoor Feb 5 '13 at 17:05
@kmansoor right... just updated my post – ben75 Feb 5 '13 at 17:17
add a comment

up vote
1
down vote
In the phase use prepare-package instead of package.

Instead of maven-antrun-plugin try to use 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>
shareimprove this answer
edited Feb 5 '13 at 16:04
answered Feb 5 '13 at 14:48

Carlos Rodrigues
1363
+1: You beat me to it! :) In addition, have a look at the list of phases described here: sonatype.com/books/mvnref-book/reference/… . – carlspring Feb 5 '13 at 15:07
@Carlos Rodrigues thanks for your comment. I have updated my question with mvn output, using prepare-package didn't help either. Can you please take a look? Thank you. – kmansoor Feb 5 '13 at 15:17 
add a comment
Your Answer


Post Your Answer
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Not the answer you're looking for? Browse other questions tagged maven maven-profiles maven-antrun-plugin or ask your own question.
asked

5 years, 5 months ago

viewed

3,898 times

active

5 years, 5 months ago

Looking for a job?
Cybersecurity Software Engineer
CyleraNew York, NY
$120K - $150KREMOTE
credis
Project Manager
EPAMShenzhen, China
RELOCATION
project-managementlifecycle
Helpdesk Support
AsiaInspection.comShenzhen, China
$16K - $20KRELOCATION
lotus-noteslotus-domino
Be one of the first applicants
HTML5 / Web Designer (React/Redux Pref.)
Core DataNo office location
REMOTE
reactjsuser-interface
Visit Chat
Related
7
How to bind maven antrun plugin to the clean phase
20
Maven: how to get a war package with resources copied in WEB-INF?
5
Files got overwritten in maven project when building a war
4
How to call maven-antrun-plugin target without attach execution to a maven phase ?
5
Calling foreach in maven-antrun-plugin
3
maven overlay of exploded war: excluding items from exploded war
4
Appengine skeleton not running
2
Maven Dependency Plugin not copying libraries to war file
0
maven-resources-plugin + profiles
1
Overlays are duplicated on classpath when war runs with maven-jetty-plugin
Hot Network Questions
I want a choice about starting a Desktop system or a Server system
Why does one person have lots of stamina and another doesn't?
Delete content of file but keep name and hierarchy
How can I write about historical realities that readers mistakenly believe are unrealistic?
Anagrams: From Food to Food
more hot questions

Maven: excluding java files in compilation

Maven: excluding java files in compilation
Ask Question
up vote
34
down vote
favorite
12
I have a folder of java sources which I wish to exclude from the compilation.

My folder is under qa/apitests/src/main/java/api/test/omi.

I added the following entry in the pom.xml under qa/bamtests but it didn't help. Is there an entry in addition I need to make?

   <build>
     <resources>
       <resource>
         <directory>src/main/java</directory>
         <includes>
           <include>**/*.properties</include>
           <include>**/*.xml</include>
           <include>**/*.xsd</include>
           <include>**/*.csv</include>
         </includes>
         <excludes>
<exclude>src/main/java/api/test/omi</exclude>
         </excludes>
       </resource>
</build>
maven
shareimprove this question
edited Jul 29 '13 at 10:03
user1907906
asked Jul 29 '13 at 9:53

constantlearner
2,91433151
Location for properties is src/main/resources for test properties src/test/resources. – khmarbaise Jul 29 '13 at 9:59
How does your project in qa/bamtests even find the sources in qa/apitests? – user1907906 Jul 29 '13 at 10:04
what about using the same pattern for excludes: <exclude>**/test/omi/**</exclude> – boskop Jul 29 '13 at 10:09
a bit off topic: your way to organize resources is just a mess. You should put it in src/main/resources (or create other directories under src/main/ ) to organize your different kind of resources – Adrian Shum Aug 12 '13 at 3:32
Read Stefan's answer about the need to remove src/main/java/ from the path! – xverges Jan 23 '15 at 15:31 
add a comment
5 Answers
active oldest votes
up vote
45
down vote
accepted
Use the Maven Compiler Plugin.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <configuration>
    <excludes>
      <exclude>**/api/test/omi/*.java</exclude>
    </excludes>
  </configuration>
</plugin>
shareimprove this answer
edited Nov 28 '16 at 16:35
community wiki
2 revs, 2 users 96%
user1907906
What does the two leading * stand for ? – Patrizio Bertoni Jun 11 '15 at 14:40
2
The ** mean 'any directories'. – user1907906 Jun 11 '15 at 14:41
2
This answer didn't work for me. Look at this stackoverflow.com/a/19713000/358013 – blueskin Sep 3 '15 at 17:17
4
For me it did not work either, I had to remove src/main/java part ... Looks like a relative path issue. Can someone please explain in detail? – Soumya Kanti Sep 7 '15 at 12:03
1
To exclude tests, see @michal-kordas 's answer on stackoverflow.com/a/32531306/517134 it worked for me – Yusuf K. Feb 8 '17 at 7:48 
show 1 more comment

Maven - exclude folder from build

Ask Question
up vote
23
down vote
favorite
4
Trying to exlcude a folder src/main/resources/scripts/ from my build but the following does not work:

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>src/main/resources/scripts/</exclude>
            </excludes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <excludes>
                    <exclude>src/main/resources/scripts/</exclude>
                </excludes>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>
Any ideas?

maven maven-compiler-plugin
shareimprove this question
asked Aug 12 '14 at 11:11

Cheetah
5,2772469133
add a comment
3 Answers
active oldest votes
up vote
40
down vote
accepted
Instead try:

<exclude>scripts/**</exclude>
The exclude is based on directory, so your construction would exclude

src/main/resources/src/main/resources/scripts
shareimprove this answer
answered Aug 12 '14 at 11:16

Robert Scholte
7,58521930
2
Thanks for the suggestion but this doesn't seem to work for me. – Cheetah Aug 12 '14 at 11:18
2
Did you do a mvn clean first? Btw, it has no effect on the manven-compiler-plugin, which only compiles .java files. The maven-resources-plugin is responsible for copying these files to the classpath. – Robert Scholte Aug 12 '14 at 11:21
I did do a mvn clean first... – Cheetah Aug 12 '14 at 11:26
4
maven.apache.org/plugins/maven-resources-plugin/examples/… gives you proper examples. ** only matches the directories, in case of files, I'd expect it to be scripts/**/* – Robert Scholte Aug 12 '14 at 12:29
Again, no joy. I am doing a mvn clean, then a mvn compile. – Cheetah Aug 12 '14 at 14:06
show 2 more comments

好了,我们将,war的执行阶段改为:deploy,又会如何呢?
试一试。

文章废弃

因为核心是要war部署多环境,不过已经找到方案了:

Maven 的 Web 项目使用 war 插件针对不同环境打包

所以经过整理,最后的代码是:

<?xml version="1.0" encoding="UTF-8"?>
<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>net.w2p</groupId>
    <artifactId>MicroWeb</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>




    <!--设置配置环境profile begin -->
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <package.environment>dev</package.environment>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <package.environment>test</package.environment>
            </properties>
        </profile>
        <profile>
            <id>product</id>
            <properties>
                <package.environment>product</package.environment>
            </properties>
        </profile>
    </profiles>
    <!--设置配置环境profile bend -->

    <!--好了,是不是经常发现pom文件一加一点东西idea的编译环境就会自动变化1.5版本的jdk??加上这个强行指定编译版本就没问题了。-->

    <build>
        <!--名字统一一下-->
        <finalName>${project.artifactId}</finalName>


        <!-- 主资源目录 -->
        <resources>
            <resource>
                <!-- 设置主资源目录 -->
                <directory>src/main/resources</directory>
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,是否对主资源目录开启资源过滤 -->
                <filtering>true</filtering>
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,只处理如下配置中包含的资源类型 -->
                <!-- 包括文件 -->
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <!-- maven default生命周期,process-resources阶段执行maven-resources-plugin插件的resources目标处理主资源目下的资源文件时,不处理如下配置中包含的资源类型(剔除下如下配置中包含的资源类型)-->
                <!-- 排除文件,这些文件打包的时候应该被排除 -->
                <excludes>
                    <exclude>profiles/dev/*.properties</exclude>
                    <exclude>profiles/test/*.properties</exclude>
                    <exclude>profiles/product/*.properties</exclude>
                    <exclude>conf/*.properties</exclude>
                </excludes>
            </resource>
        </resources>



        <plugins>




            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>

                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>


            <!--打包替换-->






            <!--打包文件替换end-->



            <!--war 打包 begin -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>

                <configuration>

                    <!--
                      Exclude JCL and LOG4J since all logging should go through SLF4J.
                      Note that we're excluding log4j-<version>.jar but keeping
                      log4j-over-slf4j-<version>.jar
                    -->

                    <webXml>web\WEB-INF\web.xml</webXml>
                    <!--指定jsp、js、css的路劲  -->
                    <warSourceDirectory>web</warSourceDirectory>
                    <packagingExcludes>
                        WEB-INF/lib/commons-logging-*.jar,
                        %regex[WEB-INF/lib/log4j-(?!over-slf4j).*.jar]
                    </packagingExcludes>
                    <webResources>
                        <resource>
                            <directory>src/main/resources/profiles/${package.environment}</directory>
                            <targetPath>WEB-INF/classes/conf</targetPath>
                            <filtering>true</filtering>
                        </resource>
                    </webResources>
                </configuration>

        </plugin>
            <!--war 打包 end -->





        </plugins>
    </build>


    <dependencies>

        <!--基础类库-->
        <dependency>
            <!--Group   net.funfunle-->
            <!--Name    baselib-->
            <groupId>net.funfunle</groupId>
            <artifactId>baselib</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <!--注意,这玩意会同时引用slf4j-api以及slf4j-log4j12,这两个是会冲突的。其中一个不引用了。-->
            <exclusions>
                <!--去除不用的依赖包-->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--app项目对应api项目 begin -->
        <dependency>
            <groupId>net.w2p</groupId>
            <artifactId>MicroBaseApi</artifactId>
            <version>1.0-SNAPSHOT</version>
            <!--注意,这玩意会同时引用slf4j-api以及slf4j-log4j12,这两个是会冲突的。其中一个不引用了。-->
            <exclusions>
                <!--去除不用的依赖包-->
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--app项目对应api项目 end-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.6.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>



        <!-- Servlet web -->
        <dependency>
            <groupId>taglibs</groupId>
            <artifactId>standard</artifactId>
            <version>1.1.2</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>



        <!--七牛 begin -->

        <dependency>
            <groupId>com.qiniu</groupId>
            <artifactId>qiniu-java-sdk</artifactId>
            <version>[7.2.0, 7.2.99]</version>
        </dependency>
        <!--七牛 end -->

        <!--apache commons 类库 begin -->
        <!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.5</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>
        <!--apache commons 类库 end -->


    </dependencies>

</project>

而且,打包的结果是:

这里写图片描述

完满解决

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值