maven 常用配置
http://www.cnblogs.com/phoebus0501/archive/2011/01/26/1945374.html
属性
maven的属性,是值的占位符,类似EL,类似ant的属性,比如${X},可用于pom文件任何赋值的位置。有以下分类:
- env.X:操作系统环境变量,比如${env.PATH}
- project.x:pom文件中的属性,比如:<project><version>1.0</version></project>,引用方式:${project.version}
- settings.x:settings.xml文件中的属性,比如:<settings><offline>false</offline></settings>,引用方式:${settings.offline}
- Java System Properties:java.lang.System.getProperties()中的属性,比如java.home,引用方式:${java.home}
- 自定义:在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引入了pom继承机制:
首先建一个com.jianxin:test-parent:0.0.1-SNAPSHOT的maven项目。
然后建一个com.jianxin:test-son:0.0.1-SNAPSHOT的maven项目(注二者是同级目录)。
打开test-son的pom.xml文件,在里面输入:
- <parent>
- <groupId>com.jianxin</groupId>
- <artifactId>test-parent</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <relativePath>../test-parent/pom.xml</relativePath>
- </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,那么就不需要再去检查本地仓库了。
这样,在父模块中定义
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.9</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies>在子模块中就会直接加上这个依赖。
问题来了:
设计模式中说过,能聚合不继承,就是因为继承有极大可能会在子类中添加子类不需要的东西。于是maven也采用了一定的机制使得父pom中的元素不被继承,但设置是继承的。
方法就是将dependencies放到dependencyManagement中,如下:
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.9</version>
- <scope>test</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
<dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement>那么,子模块在使用到junit的时候,只需要声明groupId和artifactId即可,即:
- <dependencies>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- </dependency>
- </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,即
- <dependencies>
- <dependency>
- <groupId>${project.groupId}</groupId>
- <artifactId>test-other</artifactId>
- <version>${project.version}</version>
- </dependency>
- </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 中:
- <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>
<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 :
- <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>
<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 :
- <settings>
- <pluginGroups>
- <pluginGroup>org.mortbay.jetty</pluginGroup>
- </pluginGrous>
- </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.
- <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>
<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 元素:
- <configuration>
- <type>existing</type>
- <home> D:\cmd\apache-tomcat-6.0.29</home>
- </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 项目了。
浙公网安备 33010602011771号