maven项目中配置profile

来源:互联网 发布:最好wifi密码破解软件 编辑:程序博客网 时间:2024/05/07 05:12
1. Maven常用命令

    mvn archetype:create :创建 Maven 项目
    mvn compile :编译源代码
    mvn test-compile :编译测试代码
    mvn test : 运行应用程序中的单元测试
    mvn site : 生成项目相关信息的网站
    mvn clean :清除目标目录中的生成结果
    mvn package : 依据项目生成 jar 文件
    mvn install :在本地 Repository 中安装 jar
    mvn eclipse:eclipse :生成 Eclipse 项目文件

    mvn -Dmaven.test.skip=true : 忽略测试文档编译

2.打包时的命令

在run as----run configuration

.mvn打包时动态替换成prd配置(不加mvn)
clean package -Pprd  或者clean install -Pprd

不使用profile时输入
clean install

或者

clean install -DskipTests=true


3.profile中常用标签


项目中

<span style="font-size:14px;"><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>    <parent>        <groupId>xxx</groupId>        <artifactId>root</artifactId>        <version>1.0.1</version>    </parent>    <groupId>xxx</groupId>    <artifactId>xxx</artifactId>    <version>3.0.0-SNAPSHOT</version>    <packaging>war</packaging>    <dependencies>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-core</artifactId>            <version>3.3.1.GA</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-core</artifactId>            <version>2.5.6</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-orm</artifactId>            <version>2.5.6</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-aop</artifactId>            <version>2.5.6</version>        </dependency>        .....................................        <dependency>            <groupId>com.xxx.local</groupId>            <artifactId>spring-json</artifactId>            <version>1.0</version>            <scope>system</scope>            <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/spring-json-1.1.jar</systemPath>        </dependency>        <dependency>            <groupId>net.sf.sojo</groupId>            <artifactId>sojo</artifactId>            <version>0.5.0</version>            <scope>system</scope>            <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sojo-optional-0.5.0.jar</systemPath>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.5.0</version>        </dependency>        <dependency>            <groupId>jasperreports</groupId>            <artifactId>jasperreports</artifactId>            <version>2.0.5</version>        </dependency>    </dependencies>    <properties>        <!-- 是否跳过jdk1.5测试 -->        <skip.test>true</skip.test>        <fros.version>2.0.3-SNAPSHOT</fros.version>        <jdk.version>1.5</jdk.version>    </properties>    <build>        <!-- 打包时排除context.xml文件 -->        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-war-plugin</artifactId>                <version>2.0.2</version>                <configuration>                    <webappDirectory>src/main/webapp</webappDirectory>                    <warSourceExcludes>META-INF/context.xml</warSourceExcludes>                </configuration>            </plugin>        </plugins>    </build>    <profiles>    <!-- 测试环境下的log4j设置 -->        <profile>            <id>test</id>            <properties>                <rootlog>INFO</rootlog>                <citiclog>DEBUG</citiclog>                <orglog>INFO</orglog>                <showsql>true</showsql>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/resources</directory>                           <filtering>true</filtering>                             <includes>                                 <include>properties/*.properties</include>                                 <include>messages/*.properties</include>                                 <include>conf/*.xml</include>                                <include>com/**/*.xml</include>                            </includes>                         </resource>                  </resources>            </build>            <activation>                <activeByDefault>true</activeByDefault>            </activation>        </profile>        <profile>        <!-- 打成war包时的log4j设置 -->            <id>prd</id>            <properties>                <rootlog>ERROR</rootlog>                <citiclog>ERROR</citiclog>                <orglog>ERROR</orglog>                <showsql>false</showsql>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/resources</directory>                             <filtering>true</filtering>                          <span style="color:#3333FF;"> <includes>                                 <include>properties/*.properties</include>                                 <include>messages/*.properties</include>                                 <include>conf/*.xml</include>                                 <include>com/**/*.xml</include>                             </includes> </span>                        </resource>                  </resources>            </build>            <activation>                <activeByDefault>false</activeByDefault>            </activation>        </profile>    </profiles></project></span>

resources: resource的列表,用于包括所有的资源
targetPath: 指定目标路径,用于放置资源,用于build
filtering: 是否替换资源中的属性placehold
directory: 资源所在的位置
includes: 样式,包括哪些资源
excludes: 排除的资源
testResources: 测试资源列表

还有改进的profile

<span style="font-size:14px;"><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>    <parent>        <groupId>com.xxx.root</groupId>        <artifactId>root</artifactId>        <version>1.0.1</version>    </parent>    <groupId>com.xxx</groupId>    <artifactId>xxx</artifactId>    <version>3.0.0-SNAPSHOT</version>    <packaging>war</packaging>    <dependencies>        <dependency>            <groupId>org.hibernate</groupId>            <artifactId>hibernate-core</artifactId>            <version>3.3.1.GA</version>        </dependency>        ..........................        <dependency>            <groupId>net.sf.sojo</groupId>            <artifactId>sojo</artifactId>            <version>0.5.0</version>            <scope>system</scope>            <systemPath>${basedir}/src/main/webapp/WEB-INF/lib/sojo-optional-0.5.0.jar</systemPath>        </dependency>        <dependency>            <groupId>org.slf4j</groupId>            <artifactId>slf4j-log4j12</artifactId>            <version>1.5.0</version>        </dependency>        <dependency>            <groupId>jasperreports</groupId>            <artifactId>jasperreports</artifactId>            <version>2.0.5</version>        </dependency>    </dependencies>    <properties>        <!-- 是否跳过jdk1.5测试 -->        <skip.test>true</skip.test>        <fros.version>2.0.3-SNAPSHOT</fros.version>        <jdk.version>1.5</jdk.version>    </properties>    <build>        <!-- 打包时排除context.xml文件 -->        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-war-plugin</artifactId>                <version>2.0.2</version>                <configuration>                    <webappDirectory>src/main/webapp</webappDirectory>                    <warSourceExcludes>META-INF/context.xml</warSourceExcludes>                </configuration>            </plugin>        </plugins>    </build>    <profiles>    <!-- 测试环境下的log4j设置 -->        <profile>            <id>test</id>            <properties>                <rootlog>INFO</rootlog>                <citiclog>DEBUG</citiclog>                <orglog>INFO</orglog>                <showsql>true</showsql>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/resources</directory>                           <filtering>true</filtering>  </span><pre name="code" class="html"><span style="font-size:14px;">                                                                                                 <excludes>                               <exclude>messages/*.properties</exclude>                               <exclude>conf/*.xml</exclude>                               <exclude>com/**/*.xml</exclude>                           </excludes> </span>                                </resource>                  </resources>            </build>            <activation>                <activeByDefault>true</activeByDefault>            </activation>        </profile>        <profile>        <!-- 打成war包时的log4j设置 -->            <id>prd</id>            <properties>                <rootlog>ERROR</rootlog>                <citiclog>ERROR</citiclog>                <orglog>ERROR</orglog>                <showsql>false</showsql>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/resources</directory>                             <filtering>true</filtering>                           <span style="color:#000099;"><excludes>                               <exclude>messages/*.properties</exclude>                               <exclude>conf/*.xml</exclude>                               <exclude>com/**/*.xml</exclude>                           </excludes> </span>                        </resource>                  </resources>            </build>            <activation>                <activeByDefault>false</activeByDefault>            </activation>        </profile>    </profiles></project><p></p>
 
                   

4.每次操作都要project---clean,比刷新好。1.hbm.xml版本会有大量的xml文件,放在src/main/resourse/下,路径名就是原包名的路径,1.Resource下的文件都要include进入pom.xml文件2.1.把这个文件下内容清空-----这里如果只是你的正确的文件,就不用删除,如果有重复的就删D:\eclipse-workspace\LBIS\src\main\webapp\WEB-INF\classes路径下的内容清空2.mvn打包时动态替换成prd配置(不加mvn)clean package -Pprd

5.最终改进版

<span style="font-size:12px;"><properties>        <!-- 是否跳过jdk1.5测试 -->        <skip.test>true</skip.test>        <fros.version>2.0.3-SNAPSHOT</fros.version>        <jdk.version>1.5</jdk.version>    </properties>    <build>        <!-- 打包时排除context.xml文件 -->        <plugins>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-war-plugin</artifactId>                <version>2.0.2</version>                <configuration>                    <webappDirectory>src/main/webapp</webappDirectory>                    <warSourceExcludes>META-INF/context.xml</warSourceExcludes>                </configuration>            </plugin>        </plugins>    </build>    <profiles>    <!-- 测试环境下的log4j设置 -->        <profile>            <id>test</id>            <properties>                <rootlog>INFO</rootlog>                <citiclog>DEBUG</citiclog>                <orglog>INFO</orglog>                <showsql>true</showsql>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/resources/properties</directory>                           <filtering>true</filtering>                             <targetPath>properties</targetPath>                       </resource>                      <resource>                        <directory>src/main/resources/messages</directory>                           <filtering>false</filtering>                             <targetPath>messages</targetPath>                       </resource>                      <resource>                        <directory>src/main/resources/conf</directory>                           <filtering>false</filtering>                             <targetPath>conf</targetPath>                       </resource>                      <resource>                        <directory>src/main/resources/com/citic/asp/landtrans/persistent/hbm</directory>                           <filtering>false</filtering>                             <targetPath>com/citic/asp/landtrans/persistent/hbm</targetPath>                       </resource>                  </resources>            </build>            <activation>                <activeByDefault>true</activeByDefault>            </activation>        </profile>        <profile>        <!-- 打成war包时的log4j设置 -->            <id>prd</id>            <properties>                <rootlog>ERROR</rootlog>                <citiclog>ERROR</citiclog>                <orglog>ERROR</orglog>                <showsql>false</showsql>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/resources/properties</directory>                           <filtering>true</filtering>                             <targetPath>properties</targetPath>                       </resource>                      <resource>                        <directory>src/main/resources/messages</directory>                           <filtering>false</filtering>                             <targetPath>messages</targetPath>                       </resource>                      <resource>                        <directory>src/main/resources/conf</directory>                           <filtering>false</filtering>                             <targetPath>conf</targetPath>                       </resource>                      <resource>                        <directory>src/main/resources/com/citic/asp/landtrans/persistent/hbm</directory>                           <filtering>false</filtering>                             <targetPath>com/citic/asp/landtrans/persistent/hbm</targetPath>                       </resource>                  </resources>            </build>            <activation>                <activeByDefault>false</activeByDefault>            </activation>        </profile>    </profiles></project></span>



0 0
原创粉丝点击