maven中的灵活构建

1.为了是项目的配置更加的灵活,我们可以采用maven中的属性

 

maven中的属性分别为:

 

A.内置属性:主要有两个常用内置属性 ${basedir}表示项目根目录,即包含pom.xml文件的目录,${version}表示项目的版本

 

B.pom属性:${project.artifactId} 就对应了<artifactId>元素,${project.groupId} ,${project.version}

 

常用的POM属性

 

${project.build.sourceDirectory} :  项目的主源码目录,默认为:src/main/java。

 

${project.build.testSourceDiretory} :项目的测试源码目录, 默认为:src/test/java。

 

${project.build.directory} :项目构建输出目录,默认为target。

 

${project.outputDirectory} : 项目主代码编译输出目录,默认为: target/classess。

 

${project.testOutputDirectory} : 项目主代码编译输出目录,默认为: target/test-classess。

 

${project.groupId} : 项目的groupId。

 

${project.artifactId}: 项目的artifactId。

 

${project.version} :项目的版本。

 

${project.build.finalName} :项目打包输出文件的名称,默认为${project.artifactId} -${project.artifactId}。

 

 

C.自定义属性,我们可以通过<projecties>属性完成

 

<projecties>

     <struts.version>1.3</struts.version>

</projecties>

 

 

D.Settings属性:与POM属性同理,可以使用settings,开头的属性引用setting.xml文件中XML元素的值,如常用的

 

${settings.localRepository}指向用户本地仓库的地址。

 

E. java系统属性: 所有Java系统属性都可以使用Maven属性引用,例如${user.home}指向用户目录。用户可以使用

 

mvn help :system 查看所有的java系统属性。

 

F: 环境变量属性:所有环境变量都可以使用以 env 开头的Maven属性引用。例如 ${env.JAVA_HOME}

 

 

针对不同的环境,我们为了灵活切换,可以采用profile配置

 

例如:

<!-- 启动tomcat -->
  <profile>
   <id>tomcat</id>
   <build>
    <plugins>
     <plugin>
      <groupId>org.codehaus.cargo</groupId>
      <artifactId>cargo-maven2-plugin</artifactId>
      <version>1.2.3</version>
      <executions>
       <execution>
        <id>cargo-run</id>
        <phase>pre-integration-test</phase>
        <goals>
         <goal>run</goal>
        </goals>
        <configuration>
         <container>
          <containerId>tomcat7x</containerId>
          <home>D:\apache-tomcat-7.0.29</home>
         </container>
         <configuration>
          <type>standalone</type>
          <home>${project.build.directory}/tomcat7.0.29</home>
          <properties>
           <cargo.jvmargs>
            -Xdebug
            -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787
                 </cargo.jvmargs>
          </properties>
         </configuration>
        </configuration>
       </execution>
      </executions>
     </plugin>
    </plugins>
   </build>
  </profile>

  <!-- 启动jboss -->
  <profile>
   <id>jboss</id>
   <build>
    <plugins>
     <plugin>
      <groupId>org.codehaus.cargo</groupId>
      <artifactId>cargo-maven2-plugin</artifactId>
      <version>1.2.3</version>
      <executions>
       <execution>
        <id>cargo-run</id>
        <phase>pre-integration-test</phase>
        <goals>
         <goal>run</goal>
        </goals>
        <configuration>
         <container>
          <containerId>jboss42x</containerId>
          <home>${env.JBOSS_HOME}</home>
         </container>
         <configuration>
          <type>standalone</type>
          <home>${project.build.directory}/jboss-4.2.3.GA</home>
          <properties>
           <cargo.jvmargs>
            -Xdebug
            -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8787
                 </cargo.jvmargs>
                 <cargo.servlet.port>17200</cargo.servlet.port>
          </properties>
         </configuration>
        </configuration>
       </execution>
      </executions>
     </plugin>
    </plugins>
   </build>
  </profile>

 </profiles>

 

如果采用tomcat运行则运行命令:mvn  clean install -Ptomcat,如果采用jboss运行,则运行命令:mvn  clean

 

install  -Pjboss。

 

这里可以灵活的部署和运行。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(maven)