maven 常用配置

 

 

 http://www.cnblogs.com/phoebus0501/archive/2011/01/26/1945374.html

属性

maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。有以下分类:

  1. env.X:操作系统环境变量,比如${env.PATH}
  2. project.x:pom文件中的属性,比如:<project><version>1.0</version></project>,引用方式:${project.version}
  3. settings.x:settings.xml文件中的属性,比如:<settings><offline>false</offline></settings>,引用方式:${settings.offline}
  4. Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}
  5. 自定义:在pom文件中可以:<properties><installDir>c:/apps/cargo-installs</installDir></properties>,引用方式:${installDir}

构建设置

构建有两种build标签:

<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/maven-v4_0_0.xsd">
  …
  <!– "Project Build" contains more elements than just the BaseBuild set –>
  <build>…</build>
  <profiles>
    <profile>
      <!– "Profile Build" contains a subset of "Project Build"s elements –>
      <build>…</build>
    </profile>
  </profiles>
</project>

build中的主要标签:Resources和Plugins。

Resources:用于排除或包含某些资源文件

    <resources>
      <resource>
        <targetPath>META-INF/plexus</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/plexus</directory>
        <includes>
          <include>configuration.xml</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>

Plugins:设置构建的插件

  <build>
    …
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <version>2.0</version>
        <extensions>false</extensions>
        <inherited>true</inherited>
        <configuration>
          <classifier>test</classifier>
        </configuration>
        <dependencies>…</dependencies>
        <executions>…</executions>
      </plugin>

 


 

http://blog.csdn.net/jianxin1009/article/details/17358413

maven使用基本功——pom文件优化知多少

分类: maven 387人阅读 评论(30) 收藏 举报

        使用maven进行多模块项目构建时,不得不提的就是pom文件的优化问题。

        maven引入了pom继承机制:

        首先建一个com.jianxin:test-parent:0.0.1-SNAPSHOT的maven项目。

        然后建一个com.jianxin:test-son:0.0.1-SNAPSHOT的maven项目(注二者是同级目录)。

        打开test-son的pom.xml文件,在里面输入:

 

  1. <parent>  
  2.     <groupId>com.jianxin</groupId>  
  3.     <artifactId>test-parent</artifactId>  
  4.     <version>0.0.1-SNAPSHOT</version>  
  5.     <relativePath>../test-parent/pom.xml</relativePath>  
  6. </parent>  
	<parent>
		<groupId>com.jianxin</groupId>
		<artifactId>test-parent</artifactId>
		<version>0.0.1-SNAPSHOT</version>
		<relativePath>../test-parent/pom.xml</relativePath>
	</parent>
        其中relativePath的默认值为../pom.xml,如果test-son的根目录在test-parent下,那么relativePath就可以不写了。这个路径的重要之处在于,如果专注于一个子模块的开发,那么父模块有极大可能是没有装到本地仓库的,这个路径错了,那么Maven将无法找到父pom,导致构建失败。如果根据relativePath找到父pom,那么就不需要再去检查本地仓库了。

 

        这样,在父模块中定义

 

  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.         <version>4.9</version>  
  6.         <scope>test</scope>  
  7.     </dependency>  
  8. </dependencies>  
		<dependencies>
			<dependency>
				<groupId>junit</groupId>
				<artifactId>junit</artifactId>
				<version>4.9</version>
				<scope>test</scope>
			</dependency>
		</dependencies>
        在子模块中就会直接加上这个依赖。

 

        问题来了:

        设计模式中说过,能聚合不继承,就是因为继承有极大可能会在子类中添加子类不需要的东西。于是maven也采用了一定的机制使得父pom中的元素不被继承,但设置是继承的。

        方法就是将dependencies放到dependencyManagement中,如下:        

 

  1. <dependencyManagement>  
  2.     <dependencies>  
  3.         <dependency>  
  4.             <groupId>junit</groupId>  
  5.             <artifactId>junit</artifactId>  
  6.             <version>4.9</version>  
  7.             <scope>test</scope>  
  8.         </dependency>  
  9.     </dependencies>  
  10. </dependencyManagement>  
<dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.9</version>
			<scope>test</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
        那么,子模块在使用到junit的时候,只需要声明groupId和artifactId即可,即:

 

 

  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>junit</groupId>  
  4.         <artifactId>junit</artifactId>  
  5.     </dependency>  
  6. </dependencies>  
<dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
	</dependency>
</dependencies>
        有人说这样也不省事啊,但是却不用开发子模块的人员写version和scope,还有就是对于依赖中有可能会设置的exclusions也会继承,那么那些繁杂的设置就不需要再进行重复设置了。

 

        类似的plugins有一个上级标签pluginManagement,设置还是会被继承,相对dependency来说plugins的优势更加明显,plugin的设置是比较多的,而如果plugin如果不这样设置,那么如果更改一点东西,所有的用到plugin都需要更改,那将是一个恶梦。

        这里有一个图片,是从maven实战上面截下来的,上面说明了一下都是什么样的元素都会被继承,其实不看也行,我对其总结为只要是重复的,那么就可以试着提一下,因为我们若碰到重复写的配置,那么人家肯定会将这个问题解决掉的,不然maven也不会这么火。

        但也有一个元素是这里面没有,但确定能够被继承,那就是profiles也能被继承。

=================================华丽的侵害线=======================================

        以上是对于公共部分的优化,对于我们项目中的jar的引用,我们的定义是不将其放到父pom中,而是在子项目中用到哪个就直接复用gav进行定位。但gv是用maven中内置属性project.groupId,project.version,即

 

  1. <dependencies>  
  2.     <dependency>  
  3.         <groupId>${project.groupId}</groupId>  
  4.         <artifactId>test-other</artifactId>  
  5.         <version>${project.version}</version>  
  6.     </dependency>  
  7. </dependencies>  
<dependencies>
	<dependency>
		<groupId>${project.groupId}</groupId>
		<artifactId>test-other</artifactId>
		<version>${project.version}</version>
	</dependency>
</dependencies>
        但还是需要自己设置scope的事,个人感觉这里会埋下什么伏笔,但愿不要出什么问题吧。

 

=================================华丽的侵害线=======================================

        其实优化也不难,在你会用maven将一条线全部构建成功后,其它线的构建也就是这样构建,那么你就可以以这条线的配置为基础,将第三方的全部抽取的父pom中,将子pom中除groupId、artifactId都删了,涉及每个都需要的设置信息,都放到父pom中,最后如果能运行成功,那么就没有什么问题了。

        关键是要有看到重复就不舒服的心,pom文件的优化会随着项目的进行而进一步修改有大的改动后,再进行说明。

 


 

 

 http://seanzhou.iteye.com/blog/1407524

第十二章 使用Maven构建Web应用

1.   Web 项目的 POM 中需要显示地指定打包方式为 war ,其默认的 web 资源目录为 src/main/webapp/ ,在该目录下必须包含 WEB-INF/web.xml 。

 

2.   Maven 属性 ${project.groupId} 和 ${project.version} 分别代表了当前 POM 的项目的 groupId 和 version 。

 

3.   javax.servlet.servlet-api 和 javax.servlet.jsp.jsp-api 这两个构建基本上所有 Web 项目都会依赖它们,但它们的依赖范围应该是 provided ,表示它们最终不会被打包到 war 文件中,这是因为几乎所有 Web 容器都会提供这两个类库。

 

4.   超级 POM 中定义了 fileName 元素(在 project 元素下)的默认值为 ${project.artifactId}-${project.version} ,该元素用来标识项目生成的主构件的名称。但为了在访问 web 项目页面的时候不输入冗长的地址,我们会配置 fileName 元素为项目生成更为简洁的 war 包名。

 

5.   在 web.xml 中:

Xml代码  
  1. <web-app>  
  2.   
  3.   …   
  4.   
  5.   <listener>  
  6.   
  7.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  8.   
  9.   </listener>  
  10.   
  11.   <context-param>  
  12.   
  13.     <param-name>contextConfigLocation</param-name>  
  14.   
  15.     <param-value>  
  16.   
  17.       classpath://account-persist.xml   
  18.   
  19.       classpath://account-captcha.xml   
  20.   
  21.       classpath://account-email.xml   
  22.   
  23.       classpath://account-service.xml   
  24.   
  25.     </param-value>  
  26.   
  27.   </context-param>  
  28.   
  29.   …   
  30.   
  31. </web-app>  
<web-app>

  …

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>

      classpath://account-persist.xml

      classpath://account-captcha.xml

      classpath://account-email.xml

      classpath://account-service.xml

    </param-value>

  </context-param>

  …

</web-app>

 

ContextLoaderListener 用来为 Web 项目启动 Spring 的 IoC 容器,从而实现 Bean 的注入。 contextConfigLocation 设定了 Spring 配置文件的位置。

 

 

6.   jetty-maven-plugin 能够周期性地检查 Web 项目内容,发现编译后的文件变化后自动更新到内置的 Jetty Web 容器中。可以在 POM 文件中配置 jetty-maven-plugin :

Java代码  
  1. <plugin>   
  2.   
  3.   <groupId>org.mortbay.jetty</groupId>   
  4.   
  5.   <artifactId>jetty-maven-plugin</artifactId>   
  6.   
  7.   <version>7.1.6.v20100715</version>   
  8.   
  9.   <configuration>   
  10.   
  11.     <scanIntervalSeconds>10</scanIntervalSeconds>   
  12.   
  13.     <webAppConfig>   
  14.   
  15.       <contextPath>/test</contextPath>   
  16.   
  17.     </webAppConfig>   
  18.   
  19.   </configuration>   
  20.   
  21. </plugin>  
<plugin>

  <groupId>org.mortbay.jetty</groupId>

  <artifactId>jetty-maven-plugin</artifactId>

  <version>7.1.6.v20100715</version>

  <configuration>

    <scanIntervalSeconds>10</scanIntervalSeconds>

    <webAppConfig>

      <contextPath>/test</contextPath>

    </webAppConfig>

  </configuration>

</plugin>
 

scanIntervalSeconds 的默认值为 0 ,也就是不扫描。配置 settings.xml :

 

Java代码  
  1. <settings>   
  2.   
  3.   <pluginGroups>   
  4.   
  5.     <pluginGroup>org.mortbay.jetty</pluginGroup>   
  6.   
  7.   </pluginGrous>   
  8.   
  9. </settings>   
<settings>

  <pluginGroups>

    <pluginGroup>org.mortbay.jetty</pluginGroup>

  </pluginGrous>

</settings> 
 

这样就可以使用简化的命令行调用:

mvn jetty:run –Djetty.port=9999

端口默认是 8080 。详细资料可以参考

http://wiki.eclipse.org/Jetty/Feature/Jetty_Maven_Plugin

 

7.  C argo 是一组帮助用户操作 Web 容器的工具,它能够帮助用户实现自动化部署,而且它几乎支持所有的 Web 容器,如 Tomcat 、 JBoss 、 Jetty 和 Glassfish 等。

 

8.  C argo 支持两种本地部署的模式: standalone 和 existing 。在 standalone 模式中, Cargo 会从 Web 容器的安装目录复制一份配置到用户指定的目录,然后在此基础上部署应用,每次重新构建的时候,这个目录都会被清空,所有配置被重新生成。而在 existing 模式中,用户需要指定现有的 Web 容器配置目录,然后 Cargo 会直接使用这些配置并将应用部署到其对应的位置。

 

9.

Xml代码  
  1. <plugin>  
  2.   
  3.   <groupId>org.codehaus.cargo</groupId>  
  4.   
  5.   <artifactId>cargo-maven2-plugin</artifactId>  
  6.   
  7.   <version>1.0</version>  
  8.   
  9.   <configuration>  
  10.   
  11.     <container>  
  12.   
  13.       <containerId>tomcat6x</containerId>  
  14.   
  15.       <home>D:\cmd\apache-tomcat-6.0.29</home>  
  16.   
  17.     </container>  
  18.   
  19.      <configuration>  
  20.   
  21.        <type>standalone</type>  
  22.   
  23.       <home>${project.build.directory}/tomcat6x</home>  
  24.   
  25.       <properties>  
  26.   
  27.         <cargo.servlet.port>8081</cargo.servlet.port>  
  28.   
  29.       </properties>  
  30.   
  31.     </configuration>  
  32.   
  33.   </configuration>  
  34.   
  35. </plugin>   
<plugin>

  <groupId>org.codehaus.cargo</groupId>

  <artifactId>cargo-maven2-plugin</artifactId>

  <version>1.0</version>

  <configuration>

    <container>

      <containerId>tomcat6x</containerId>

      <home>D:\cmd\apache-tomcat-6.0.29</home>

    </container>

     <configuration>

       <type>standalone</type>

      <home>${project.build.directory}/tomcat6x</home>

      <properties>

        <cargo.servlet.port>8081</cargo.servlet.port>

      </properties>

    </configuration>

  </configuration>

</plugin> 
 

configuration 元素的 home 子元素表示复制容器配置到什么位置,这里的值 ${project.build.directory} 表示构建输出目录,即 target/ 。 container 元素下的 containerId 表示容器的类型。 Cargo 默认会让 Web 容器监听 8080 端口。要使用 existing 模式只需更改 configuration 元素:

Xml代码  
  1. <configuration>  
  2.   
  3.   <type>existing</type>  
  4.   
  5.   <home> D:\cmd\apache-tomcat-6.0.29</home>  
  6.   
  7. </configuration>   
<configuration>

  <type>existing</type>

  <home> D:\cmd\apache-tomcat-6.0.29</home>

</configuration> 
 

这里 home 子元素表示现有的 Web 容器目录。运行 mvn cargo:start 之后就会在 Tomcat 的 webapps 子目录下看到被部署的 web 项目。

 

10.  对 于远程部署, container 元素的 type 子元素值必须为 remote (默认值为 installed )。 configuration 元素的 type 子元素值为 runtime ,表示既不使用独立的容器配置,也不使用本地现有的容器配置,而是依赖一个已运行的容器。 Properties 元素用来声明一些容器热部署相关的配置,可以参考: http://cargo.codehaus.org/Maven2+plugin

运行 mvn cargo:redeploy 就能重新部署 web 项目了。

 

 

 


 

maven pom继承   

 
pom 标记
groupId 项目组名称
artifactId 制品名
version  版本号
name pom的名称
artifactid和groupId标识pom的唯一
 
项目pom继承
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-module</artifactId>
  <version>1</version>
  <packaging>pom</packaging>
  
   <modules>
      <module>server</module>-->子项目的目录(这种写法表表示子项目必须在父目录内)
      <module>../server</module>-->子项目的目录(这种写法表示同级子项目)
   </modules>
 
   <!--参数-->
    <properties>
        <spring.version>3.0.5.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><!--build编码-->
    </properties>
</project>
 
 
 
子项目pom继承父pom
<project>
  <parent>
    <groupId>com.mycompany.app</groupId><--父pom的groupId
    <artifactId>my-app</artifactId><--父pom的artifactId
    <version>1</version>
<relativePath>../parent/pom.xml</relativePath><--对于同级的pom要指定位置
  </parent>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-server</artifactId>
  <version>1</version>
</project>
posted @ 2014-01-14 17:28  ftefno  阅读(99)  评论(0)    收藏  举报