??xml version="1.0" encoding="utf-8" standalone="yes"?>福利精品一区,911亚洲精品,成人一区二区视频http://www.aygfsteel.com/sealyu/category/43349.html--- The devil's in the Detailszh-cnMon, 08 Feb 2010 23:45:17 GMTMon, 08 Feb 2010 23:45:17 GMT60ȀzMaven profile的几U方??http://www.aygfsteel.com/sealyu/archive/2010/02/08/312320.htmlsealsealMon, 08 Feb 2010 05:58:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312320.htmlhttp://www.aygfsteel.com/sealyu/comments/312320.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312320.html#Feedback1http://www.aygfsteel.com/sealyu/comments/commentRss/312320.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/312320.html

首先单介l下 Maven ?profile 是什么。对于h来说Qprofile 是指人的肖像Q轮廓,比如论坛里每个h注册了帐号后Q可以设|自q profileQ放上照片,介绍{等。对?Maven 来说又是怎样呢?整个目定义好了目对象模型QPOMQ,像论坛为每个h提供了默认的行ؓ功能Q如果我x变我机器上的 POM 呢?q时可以?profile。下面D个例子:

Xml代码
  1. <profiles>  
  2.   <profile>  
  3.     <id>jdk16</id>  
  4.     <activation>  
  5.       <jdk>1.6</jdk>  
  6.     </activation>  
  7.     <modules>  
  8.       <module>simple-script</module>  
  9.     </modules>  
  10.   </profile>  
  11. </profiles>  

q个 profile 的意思是Q当机器上的 JDK ?.6的时候,构徏 simple-script q个子模块,如果?.5或?.4Q那׃构徏Q这?profile 是由环境自动Ȁzȝ?/p>

我们需要在合适的地方使用合适的 profile Qƈ且在合适的时候用合适的方式其Ȁz,你不能在构徏服务器上Ȁz非公共?profileQ你也不能要求开发h员写很复杂的命o来用常规的 profile。因此这里介l一下几U?profile 的激zL式?/p>

1. Ҏ(gu)环境自动ȀzR?/p>

如前一个例子,?JDK ?.6的时候,Maven ׃自动构徏 simple-script 模块。除?JDK 之外Q我们还可以Ҏ(gu)操作pȝ参数?Maven 属性等来自动激z?profileQ如Q?/p>

Xml代码
  1. <profile>  
  2.   <id>dev</id>  
  3.   <activation>  
  4.     <activeByDefault>false</activeByDefault>  
  5.     <jdk>1.5</jdk>  
  6.     <os>  
  7.       <name>Windows XP</name>  
  8.       <family>Windows</family>  
  9.       <arch>x86</arch>  
  10.       <version>5.1.2600</version>  
  11.     </os>  
  12.     <property>  
  13.       <name>mavenVersion</name>  
  14.       <value>2.0.5</value>  
  15.     </property>  
  16.     <file>  
  17.       <exists>file2.properties</exists>  
  18.       <missing>file1.properties</missing>  
  19.     </file>  
  20.   </activation>  
  21.   ...  
  22. </profile>  

2. 通过命o行参数激zR?/p>

q是最直接和最单的方式Q比如你定义了一个名?myProfile ?profileQ你只需要在命o行输?mvn clean install -Pmyprofile p其Ȁz,q种方式的好处很明显Q但是有一个很大的弊端Q当 profile 比较多的时候,在命令行输入q写 -P 参数会让得厌烦,所以,如果你一直用q种方式Q觉得厌烦了Q可以考虑使用其它自动Ȁzȝ方式?/p>

3. 配置默认自动ȀzR?/p>

Ҏ(gu)很简单,在配|?profile 的时候加上一条属性就可以了,如:

Xml代码
  1. <profile>  
  2.   <id>dev</id>  
  3.   <activation>  
  4.     <activeByDefault>true</activeByDefault>  
  5.   </activation>  
  6.   ...  
  7. </profile>  

在一个特D的环境下,配置默认自动Ȁzȝ profile 覆盖默认?POM 配置Q非常简单有效?/p>

4. 配置 settings.xml 文g profile ȀzR?/p>

settings.xml 文g可以?~/.m2 目录下,为某个用L自定义行为服务,也可以在 M2_HOME/conf 目录下,为整台机器的所有用h务。而前者的配置会覆盖后者。同理,?settings.xml Ȁzȝ profile 意在为用h者整个机器提供特定环境配|,比如Q你可以在某台机器上配置一个指向本地数据库 URL ?profileQ然后用该机器?settings.xml Ȁzd。激zL式如下:

Xml代码
  1. <settings>  
  2.   ...  
  3.   <activeProfiles>  
  4.     <activeProfile>local_db</activeProfile>  
  5.   </activeProfiles>  
  6. </settings>  

Maven 提供?profile 功能非常强大和灵z,用得好的话,可以有效的隔d多特D的配置Q得整个项目能在不同环境中利的构建。但是,强大和灵zd来得问题是相寚w掌握Q希望本文能?Maven 使用者有帮助?/p>

seal 2010-02-08 13:58 发表评论
]]>
maven 中引用外部properties文ghttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312298.htmlsealsealMon, 08 Feb 2010 02:42:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312298.htmlhttp://www.aygfsteel.com/sealyu/comments/312298.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312298.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/312298.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/312298.html 1.如果需要引用外部propertiest文g中的属性|需要在pom.xml中添加对外部文g的引用。例如,创徏一个外部资源文件pom.propertiesQ?/a>
# filter.properties
my.filter.value=hello!

对它的引用d到pom.xml中:

<build>
<filters>
<filter>${baseDir}/pom.properties</filter>
</filters>
</build>
不过q种方式要启动资源过滤,q设|好对应的文件夹才行Q相对较ȝ? 2.使用插gQ?br /> maven-properties-plugin  使用Ҏ(gu)可以看插件的官方地址Qhttp://haroon.sis.utoronto.ca/zarar/properties-maven-plugin/index.html

properties-maven-plugin  使用Ҏ(gu)可以看插件的官方地址Qhttp://mojo.codehaus.org/properties-maven-plugin/


seal 2010-02-08 10:42 发表评论
]]>
MavenPropertiesGuidehttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312293.htmlsealsealMon, 08 Feb 2010 02:12:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312293.htmlhttp://www.aygfsteel.com/sealyu/comments/312293.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/02/08/312293.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/312293.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/312293.htmlIntro

Because I couldn't find a clear overview of which properties are availabe in maven2 I started this page. It is a collection of things found in the offcial maven documentation and postings to the maven user mailing list.

Note: In Maven 3.0, all pom.* properties are deprecated. Use project.* instead!

Build in properties

  • ${basedir} represents the directory containing pom.xml
  • ${version} equivalent to ${project.version} or ${pom.version}

Pom/Project properties

All elements in the pom.xml, can be referenced with the project. prefix or using pom. as prefix. This list is just an example of some commonly used elements.

  • ${project.build.directory} results in the path to your "target" dir, this is the same as ${pom.project.build.directory}
  • ${project.build.outputDirectory} results in the path to your "target/classes" dir
  • ${project.name} or ${pom.name} refers to the name of the project.
  • ${project.version} or ${pom.version} refers to the version of the project.
  • ${project.build.finalName} refers to the final name of the file created when the built project is packaged

Local user settings

Similarly, values in the user's settings.xml can be referenced using property names with settings. prefix.

  • ${settings.localRepository} refers to the path of the user's local repository.
  • ${maven.repo.local} also works for backward compatibility with maven1 ??

Environment variables

Environment variables can be referenced using the env prefix

  • ${env.M2_HOME} returns the Maven2 installation path.
  • ${java.home} specifies the path to the current JRE_HOME environment use with relative paths to get for example:
    <jvm>${java.home}../bin/java.exe</jvm>

Java system properties

All Java System Properties defined by the JVM.

Custom properties in the POM

User defined properties in the pom.xml.

<project>
...
<properties>
<my.filter.value>hello</my.filter.value>
</properties>
...
</project>
  • ${my.filter.value} will result in hello if you inserted the above XML fragment in your pom.xml

Parent Project variables

How can parent project variables be accessed?

You can use the prefix: ${project.parent}.

A good way to determine possible variables is to have a look directly at the API. I'm currently using Maven 2.2.1, and to access the Parent you can use ${project.parent}. This will return an org.apache.maven.project.MavenProject instance.

To access the parent version: ${parent.version}.

Reflection Properties

The pattern ${someX.someY.someZ} can simply sometimes mean getSomeX().getSomeY().getSomeZ(). Thus, properties such as ${project.build.directory} is translated to getProject().getBuild().getDirectory().



seal 2010-02-08 10:12 发表评论
]]>
PermGen space exceptions with Maven due to memory leakhttp://www.aygfsteel.com/sealyu/archive/2010/01/22/310500.htmlsealsealFri, 22 Jan 2010 03:43:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/22/310500.htmlhttp://www.aygfsteel.com/sealyu/comments/310500.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/22/310500.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/310500.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/310500.htmlMaybe you know the situation: you are developing a web application using the Maven build system along with an application server like Tomcat or Jetty and a corresponding plugin (e.g. the all famous jetty-maven-plugin).

One of the coolest features of these containers is the hot-deployment.

However, every second time a hot deployment is executed, it crashes due to a PermGen space Exception or a similar OutOfMemoryException.

The cause for this behaviour is a memory leak with the classloaded of these containers. If you are interested in the details, you may read this article on the Classloader Memory Leaks.

Actually, you can’t fix this bug. But there is a way to prevent it from beeing so painful. And it’s pretty easy: simply assign more memory to your JVM!

In order to do so, create a new environment variable named MAVEN_OPTS. Maybe you already defined this variable. What you have to do now is to adjust the MaxPermSize (and the other parameters, too) to a proper value according to your machine’s memory.
For instance, set the MAVEN_OPTS like this:

-Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m

Having 2 GB of RAM, these values are a good start.

Now you should be rid of the nasty PermGen space exceptions.



Related:
Catching Exceptions with an UncaughtExceptionHandler in Java
Problems with Assertions in JUnit tests using Eclipse
.NET Color Constants

seal 2010-01-22 11:43 发表评论
]]>
解决Maven中OutOfMemory错误(?http://www.aygfsteel.com/sealyu/archive/2010/01/19/310030.htmlsealsealTue, 19 Jan 2010 00:55:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/19/310030.htmlhttp://www.aygfsteel.com/sealyu/comments/310030.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/19/310030.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/310030.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/310030.html当Maven目很大Q或者你q行诸如 mvn site q样的命令的时候,mavenq行需要很大的内存Q在默认配置下,可能遇到java的堆溢出。如Q?/p>
[INFO] Building jar: /home/dl9pf/svn/mindquarry/mindquarry-jcr/mindquarry-jcr-changes/target/mindquarry-migration-with-dependencies.jar
[INFO] ------------------------------------------------------------------------
[ERROR] FATAL ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Java heap space
[INFO] ------------------------------------------------------------------------
[INFO] Trace
java.lang.OutOfMemoryError: Java heap space
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:99)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:518)
...
at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7 minutes 14 seconds
[INFO] Finished at: Wed Sep 05 07:44:55 CEST 2007
[INFO] Final Memory: 37M/63M
[INFO] ------------------------------------------------------------------------

解决的方法是调整java的堆大小的倹{?/p>

Windows环境?

扑ֈ文g%M2_HOME%"bin"mvn.bat Q这是启动Maven的脚本文Ӟ在该文g中你能看到有一行注释ؓQ?/strong>

  @REM set MAVEN_OPTS=-Xdebug -Xnoagent -Djava.compiler=NONE...

它的意思是你可以设|一些Maven参数Q我们就在注释下面加入一行:

set MAVEN_OPTS= -Xms128m -Xmx512m

之后Q当你运行Maven命o?mvn -version 的时候,你会看到如下的输出:

E:"test>mvn -version

E:"test>set MAVEN_OPTS= -Xms128m -Xmx512m
Maven version: 2.0.9
Java version: 1.6.0_07
OS name: "windows 2003" version: "5.2" arch: "x86" Family: "windows"

我们看到Q配|的Maven选项生效了,OutOfMemoryError也能得以相应的解冟?/strong>

 

 

Linux环境?

也可以通过讄环境变量解决该问题, 如,~辑文g /etc/profile 如下

MAVEN_OPTS=-Xmx512m
export JAVA_HOME MAVEN_HOME MAVEN_OPTS JAVA_BIN PATH CLASSPATH

 

 

如果你用Hudson

?Hudson + Maven做持l集成,q不q怹遇到了类似的错误Q那么上qCU方式都不再v作用了,因ؓHudson使用自己的maven-agent来启动MavenQ不会去调用Maven的脚本,自然相应的配|也无效了?/strong>

好在Hudson也给为我们提供了配置点,在Hudson的项目配|页面中Q有一块Build区域Q这里我们已l? 讄了Root Pom和Goals。注意该区域的右下角有一?Advanced..."按钮Q点M看到MAVEN_OPTS输入框,q里输入"-Xmx512m"? OK了?/strong>

 

m2eclipse?/strong>

cM以上的方法都会失效,所qm2eclipse提供了配|点。步骤如下:

目上右?-> Run As -> Run Configurations -> Maven Build 上右?-> New

q时会看C个mavenq行配置对话框,q里面其它的配置我不多解释了Qؓ了解军_存溢出的问题Q我们可以选择W二个TAB: JREQ然后在VM arguments中输入配|如Q?Xms128m -Xmx512m?/strong>

 


q有一U说法:

~辑maven"bin中的mvn文gQ?

  • 在exec "$JAVACMD" "之后加上 -Xmx256m "卛_?
  • 形式如:

      exec "$JAVACMD" "

    -Xmx256m "



seal 2010-01-19 08:55 发表评论
]]>
maven-resources-plugin的编码问?/title><link>http://www.aygfsteel.com/sealyu/archive/2010/01/11/309023.html</link><dc:creator>seal</dc:creator><author>seal</author><pubDate>Mon, 11 Jan 2010 06:55:00 GMT</pubDate><guid>http://www.aygfsteel.com/sealyu/archive/2010/01/11/309023.html</guid><wfw:comment>http://www.aygfsteel.com/sealyu/comments/309023.html</wfw:comment><comments>http://www.aygfsteel.com/sealyu/archive/2010/01/11/309023.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/sealyu/comments/commentRss/309023.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/sealyu/services/trackbacks/309023.html</trackback:ping><description><![CDATA[今天H然在项目构建时发现有一条警告信息:<br /> Using platform encoding (GBK actually) to copy filtered resources<br /> <br /> ׃目中统一使用UTF-8~码Q所以这个地Ҏ(gu)个潜在的问题Q看了文档发玎ͼ如果不显式设|,maven会用^台的默认~码Q中文系l就是GBK了?br /> 修改如下Q?br /> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> --><span style="color: #0000ff;"><</span><span style="color: #800000;">plugin</span><span style="color: #0000ff;">></span><span style="color: #000000;">    </span><span style="color: #0000ff;"><br />     <</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span><span style="color: #000000;">org.apache.maven.plugins</span><span style="color: #0000ff;"></</span><span style="color: #800000;">groupId</span><span style="color: #0000ff;">></span><span style="color: #000000;">  <br />     </span><span style="color: #0000ff;"><</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span><span style="color: #000000;">maven-resources-plugin</span><span style="color: #0000ff;"></</span><span style="color: #800000;">artifactId</span><span style="color: #0000ff;">></span><span style="color: #000000;">  <br />     </span><span style="color: #0000ff;"><</span><span style="color: #800000;">configuration</span><span style="color: #0000ff;">></span><span style="color: #000000;">  <br />          </span><span style="color: #0000ff;"><</span><span style="color: #800000;">encoding</span><span style="color: #0000ff;">></span><span style="color: #000000;">UTF-8</span><span style="color: #0000ff;"></</span><span style="color: #800000;">encoding</span><span style="color: #0000ff;">></span><span style="color: #000000;">  <br />     </span><span style="color: #0000ff;"></</span><span style="color: #800000;">configuration</span><span style="color: #0000ff;">></span><span style="color: #000000;">  <br /> </span><span style="color: #0000ff;"></</span><span style="color: #800000;">plugin</span><span style="color: #0000ff;">></span><span style="color: #000000;">   <br /> </span></div> <br /> <br /> <img src ="http://www.aygfsteel.com/sealyu/aggbug/309023.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/sealyu/" target="_blank">seal</a> 2010-01-11 14:55 <a href="http://www.aygfsteel.com/sealyu/archive/2010/01/11/309023.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>How do I include tools.jar in my dependencies?http://www.aygfsteel.com/sealyu/archive/2010/01/10/308902.htmlsealsealSun, 10 Jan 2010 03:50:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/10/308902.htmlhttp://www.aygfsteel.com/sealyu/comments/308902.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/10/308902.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/308902.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/308902.html The following code includes tools.jar for JDKs on Windows, Linux and Solaris (it is already included in the runtime for Mac OS X and some free JDKs).

...
<profiles>
<profile>
<id>default-tools.jar</id>
<activation>
<property>
<name>java.vendor</name>
<value>Sun Microsystems Inc.</value>
</property>
</activation>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.4.2</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
</profile>
</profiles>
...


seal 2010-01-10 11:50 发表评论
]]>
Missing indirectly referenced artifact com.sun:tools:jar:1.5.0:systemhttp://www.aygfsteel.com/sealyu/archive/2010/01/10/308896.htmlsealsealSun, 10 Jan 2010 03:02:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/10/308896.htmlhttp://www.aygfsteel.com/sealyu/comments/308896.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/10/308896.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/308896.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/308896.htmlhttp://livingash.wordpress.com/2007/08/16/struts-2-and-sun-micros-toolsjar-in-eclipse-wtp/

The configuration free classic way to start Eclipse with desired VM.

$>eclipse -vm "C:\Program Files\Java\jdk1.6.0_01\bin"

q个问题一般是׃使用了struts2引v的:
用了m2eclipse 插g后,使用struts2Ӟ需要依?com.sun ?default-tools.jarQ见pom.xml





<?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/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>cn.tianxing.gehouse.admin</groupId>

  <artifactId>admin.gehouse.cn</artifactId>

  <name>admin.gehouse.cn</name>

  <version>0.0.1</version>

  <description>admin.gehouse.cn</description>

  <url>http://admin.gehouse.cn</url>

  <issueManagement>

    <system>jira</system>

    <url>http://jira.gehouse.cn:8080/BrowseProject.jspa?id=10000</url>

  </issueManagement>

  <ciManagement>

    <system>continuum</system>

    <url>http://ci.gehouse.cn:8090/continuum</url>

    <notifiers>

      <notifier>

        <type>msn</type>

        <sendOnSuccess>false</sendOnSuccess>

        <sendOnWarning>false</sendOnWarning>

        <configuration>

          <address>kylindai@hotmail.com</address>

        </configuration>

      </notifier>

      <notifier>

        <type>msn</type>

        <sendOnSuccess>false</sendOnSuccess>

        <sendOnWarning>false</sendOnWarning>

        <configuration>

          <address>larrin2002@msn.com</address>

        </configuration>

      </notifier>

      <notifier>

        <type>msn</type>

        <sendOnSuccess>false</sendOnSuccess>

        <sendOnWarning>false</sendOnWarning>

        <configuration>

          <address>manjingtou-ren@hotmail.com</address>

        </configuration>

      </notifier>

    </notifiers>

  </ciManagement>

  <inceptionYear>2007-05</inceptionYear>

  <developers>

    <developer>

      <name>kylin.dai</name>

      <email>kylin.dai@gehouse.cn</email>

    </developer>

    <developer>

      <name>larrin.luo</name>

      <email>larrin.luo@gehouse.cn</email>

    </developer>

    <developer>

      <name>jason.ren</name>

      <email>jason.ren@gehouse.cn</email>

    </developer>

  </developers>

  <scm>

    <connection>scm:svn:svn://svn.gehouse.cn/sandbox/kylin/admin.gehouse.cn/trunk</connection>

  </scm>

  <organization>

    <name>gehouse.cn</name>

    <url>http://www.gehouse.cn</url>

  </organization>

  <build>

    <plugins>

      <plugin>

        <artifactId>maven-compiler-plugin</artifactId>

        <version>2.0.2</version>

        <configuration>

          <source>1.6</source>

          <target>1.6</target>

        </configuration>

      </plugin>

    </plugins>

  </build>

  <profiles>

    <profile>

      <id>default-tools.jar</id>

      <activation>

        <property>

          <name>java.vendor</name>

          <value>Sun Microsystems Inc.</value>

        </property>

      </activation>

      <dependencies>

        <dependency>

          <groupId>com.sun</groupId>

          <artifactId>tools</artifactId>

          <version>1.5.0</version>

          <scope>system</scope>

          <systemPath>${java.home}/../lib/tools.jar</systemPath>

        </dependency>

      </dependencies>

    </profile>

  </profiles>

  <dependencies>

    <dependency>

      <groupId>log4j</groupId>

      <artifactId>log4j</artifactId>

      <version>1.2.14</version>

    </dependency>

    <dependency>

      <groupId>javax.servlet</groupId>

      <artifactId>servlet-api</artifactId>

      <version>2.5</version>

    </dependency>

    <dependency>

      <groupId>dom4j</groupId>

      <artifactId>dom4j</artifactId>

      <version>1.6.1</version>

    </dependency>

    <dependency>

      <groupId>com.ibatis</groupId>

      <artifactId>ibatis</artifactId>

      <version>2.3.0.667</version>

    </dependency>

    <dependency>

      <groupId>junit</groupId>

      <artifactId>junit</artifactId>

      <version>4.1</version>

    </dependency>

    <dependency>

      <groupId>mysql</groupId>

      <artifactId>mysql-connector-java</artifactId>

      <version>5.0.5</version>

    </dependency>

    <dependency>

      <groupId>org.extremecomponents</groupId>

      <artifactId>extremecomponents</artifactId>

      <version>1.0.1</version>

    </dependency>

    <dependency>

      <groupId>commons-dbcp</groupId>

      <artifactId>commons-dbcp</artifactId>

      <version>1.2.1</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring</artifactId>

      <version>2.0.5</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-mock</artifactId>

      <version>2.0.5</version>

    </dependency>

    <dependency>

      <groupId>org.springframework</groupId>

      <artifactId>spring-aspects</artifactId>

      <version>2.0.5</version>

    </dependency>

    <dependency>

      <groupId>org.apache.struts</groupId>

      <artifactId>struts2-core</artifactId>

      <version>2.0.6</version>

    </dependency>

  </dependencies>

  <reporting>

    <plugins>

      <plugin>

        <artifactId>maven-changelog-plugin</artifactId>

      </plugin>

      <plugin>

        <artifactId>maven-checkstyle-plugin</artifactId>

      </plugin>

      <plugin>

        <artifactId>maven-pmd-plugin</artifactId>

      </plugin>

      <plugin>

        <artifactId>maven-changes-plugin</artifactId>

        <reportSets>

          <reportSet>

            <reports>

              <report>jira-report</report>

            </reports>

          </reportSet>

        </reportSets>

      </plugin>

    </plugins>

  </reporting>

</project>





q个pom.xml 在eclipse里加载的时候L报告Q?br />


Missing:

1) com.sun:tools:jar:1.4.2

Try downloading the file manually from the project website.

Then, install it using the command:



mvn install:install-file -DgroupId=com.sun -DartifactId=tools "

-Dversion=1.4.2 -Dpackaging=jar -Dfile=/path/to/file

Path to dependency:



原来Q是${java.home}在作怪,eclipse 没有使用 JAVA_HOME

默认Qeclipse 使用 C:"windows"system32"javaw.exe 作ؓ JVMQ当然找不到tools.jar



解决Ҏ(gu)如下Q?br />
修改 eclipse.exe 目录下的 eclipse.ini 指定vmQ加入红色黑体字部分Q注?-vm后面不能有空根{?br />


-vm

D:"Program Files"Java"jdk1.6.0_01"bin"javaw.exe

-vmargs

-Xms128m

-Xmx512m



再次启动 eclipseQ问题解冟?br />


上面的pom.xml文gQ也是一?Mavne2 & Continuum 的例子,由我写的 Maven2 &
Continuum 持箋整合 (3)Q发布时Q网l故障丢׃Q也不打重新再写一了Q上q的pom.xmlQ可以在continuum?add




maven2 projects 里,d使用Q设?schedulerQ可以设1时 build 一ơ,半小?test 一ơ,{不同的d




seal 2010-01-10 11:02 发表评论
]]>
maven2 建立l一的管理站点(转)http://www.aygfsteel.com/sealyu/archive/2010/01/08/308736.htmlsealsealFri, 08 Jan 2010 08:33:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/08/308736.htmlhttp://www.aygfsteel.com/sealyu/comments/308736.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/08/308736.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/308736.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/308736.html 使用maven2的另外一个好处,是有了l一的入口,用于察看目的进展情c这主要包括了项目的介绍Q成员介l,以及相关的项目的文档Q当然也包括目的所有进展报表?br />     下面Q通过实例来介l如何配|mavenQ来产生目站点?br />     首先创徏相关的site
mvn archetype:create "
      -DarchetypeGroupId=org.apache.maven.archetypes "
      -DarchetypeArtifactId=maven-archetype-site "
      -DgroupId=com.mycompany.app "
     -DartifactId=my-app-site
  你会看到如下的目录:
my-app-site
|-- pom.xml
`-- src
    `-- site
        |-- apt
        |   |-- format.apt
        |   `-- index.apt
        |-- fml
        |   `-- faq.fml
        |-- fr
        |   |-- apt
        |   |   |-- format.apt
        |   |   `-- index.apt
        |   |-- fml
        |   |   `-- faq.fml
        |   `-- xdoc
        |       `-- xdoc.xml
        |-- xdoc
        |   `-- xdoc.xml
        |-- site.xml
        `-- site_fr.xml
    当然也可以不用通过此来生成站点Q直接在原有的src目录中增加site目录p。当ӞZ化操作,一般先生成相应的site目录Q然后拷贝到相应的src目录中?br /> 修改pom文g
增加以下内容
        <!-- 配置发布站点url -->
           <distributionManagement>
                 <site>
                   <id>website</id>
                   <url>scp://webhost.company.com/www/website</url>
              </site>
           </distributionManagement>
           <build>
              <plugins>
           <!-- 配置site 的国际化Q默认ؓen,frQ更改ؓzh_CNQ以及设|编码格式,默认utf-8 -->
              <plugin>
                 <artifactId>maven-site-plugin</artifactId>
                <configuration>
                    <locales>zh_CN</locales>
                    <outputEncoding>GBK</outputEncoding>
                    </configuration>
                  </plugin>
             </plugins>
          </build>

在site 目录下,最重要的就是site.xml文g?br /> site.xml描述了主要的site布局Q例子如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<project name="Maven">
  <!-- left banner-->
  <bannerLeft>
    <name>Maven</name>
    <src>http://maven.apache.org/images/apache-maven-project.png</src>
    <href>http://maven.apache.org/</href>
  </bannerLeft>
  <!-- right banner-->
  <bannerRight>
    <src>http://maven.apache.org/images/maven-small.gif</src>
  </bannerRight>
  <body>
    <!-- 默认的link -->
    <links>
      <item name="Apache" />
      <item name="Maven 1.0" />
      <item name="Maven 2" />
    </links>
   <!-- 菜单?-->
    <menu name="Maven 2.0">
      <item name="Introduction" href="index.html"/>
      <item name="Download" href="download.html"/>
      <item name="Release Notes" href="release-notes.html" />
      <item name="General Information" href="about.html"/>
      <item name="For Maven 1.0 Users" href="maven1.html"/>
      <item name="Road Map" href="roadmap.html" />
    </menu>
    <menu ref="reports" />
    ...
  </body>
</project>

配置站点文g
maven 支持以下的文档:
xdoc格式Q用简单的xml格式
apt格式Qlike wiki格式的纯文本
fml格式Q?faq格式
docBook
一般采用apt文gQ默认的apt都是iso-8859-1的,如果需要支持中文,需要用native2ascii命o来{换?br />
报表Q?br /> 站点主要的配|,目前不错的报表插Ӟ包括javadoc,pmd,checkstyle,Surefire test,source xrefQtag list{?br /> 下面详细介绍q些报表Q?br /> javadocQ对于团队内部来_javadoc是比较重要的Q当然前提是能好好的写javadoc?br /> 配置如下Q在reporting中增加如下的插g

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-javadoc-plugin</artifactId>
        <configuration>
          <links>
            <link>http://java.sun.com/j2se/1.4.2/docs/api</link>
            <link>http://plexus.codehaus.org/ref/1.0-alpha-9/apidocs</link>
          </links>
          <aggregate>true</aggregate>
        </configuration>
      </plugin>

pmdQ一个不错的代码查工兗?br />       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-pmd-plugin</artifactId>
        <configuration>
          <rulesets>
            <ruleset>/rulesets/basic.xml</ruleset>
            <ruleset>/rulesets/imports.xml</ruleset>
            <ruleset>/rulesets/unusedcode.xml</ruleset>
            <ruleset>/rulesets/finalizers.xml</ruleset>
            <ruleset>/rulesets/controversial.xml</ruleset>
            <ruleset>/rulesets/strings.xml</ruleset>
            <ruleset>/rulesets/strictexception.xml</ruleset>
            <ruleset>/rulesets/optimizations.xml</ruleset>
            <ruleset>/rulesets/naming.xml</ruleset>
          </rulesets>
          <linkXref>true</linkXref>
          <sourceEncoding>GBK</sourceEncoding>
          <minimumTokens>100</minimumTokens>
          <!--支持jdk 1.5 -->
          <targetJdk>1.5</targetJdk>
        </configuration>
      </plugin>

checkstyle,主要的代码格式工?和pmd相比Q更注重代码的格式?br />       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-checkstyle-plugin</artifactId>
        <configuration>
          <configLocation>sun_checks.xml</configLocation>
        </configuration>
      </plugin>

Surefire testQ单元测试结果报?br />       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-report-plugin</artifactId>
      </plugin>

source xref,在网站上直接察看java源代?br />
     <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jxr-plugin</artifactId>
      </plugin>

tag listQ用于查扑֜代码中预定义的标{,如todo
     <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>taglist-maven-plugin</artifactId>
        <configuration>
          <tags>
            <tag>TODO</tag>
            <tag>@todo</tag>
            <tag>FIXME</tag>
          </tags>
        </configuration>
      </plugin>

基本的站点已l成型了Q下面就是生成了?br /> 执行mvn site 生成目录Q或者直接执?mvn site-deploy 发布站点

seal 2010-01-08 16:33 发表评论
]]>
How to create a Maven web app and deploy to Tomcat - fast http://www.aygfsteel.com/sealyu/archive/2010/01/08/308706.htmlsealsealFri, 08 Jan 2010 05:46:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/08/308706.htmlhttp://www.aygfsteel.com/sealyu/comments/308706.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/08/308706.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/308706.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/308706.htmlProcedure

Prerequisites and Assumptions

  • Maven is already installed
  • A local instance of Tomcat is already installed and configured to run on port 8080
  • Optional - Eclipse is installed

Step One - Prepare the Tomcat Manager application

In order to deploy a web app to your Tomcat server, you will need to ensure that you can access the Tomcat Manager application at: http://localhost:8080/manager/html. Typically, you just need to ensure that your <tomcat>/conf/tomcat-users.xml file has the following defined:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="admin" password="admin" roles="admin,manager"/>
</tomcat-users>

In this case, we will be logging in to the Tomcat Manager app using:

username admin
password admin
Back to Top

Step Two - Create a New Web App Using Maven

Next, we will use a Maven archetype to generate a new web application project.

Assuming that you will primarily use Eclipse as your IDE, you may wish to start the project within Eclipse. We'll start the project within Eclipse and then use Maven to fill the project in with the proper Maven web app structure.

  • In Eclipse create a new General project. New > Project... > General > Project
  • Project name: "sw" (name it what you wish, but remember to replace 'sw' with your project name in all instances where that appears in this document from here on out.)

The steps above just create a General project in your Eclipse workspace (e.g. a folder with a .project settings file inside of it).

Next, open a command prompt, cd into the 'sw' project directory, and then execute the following Maven command (all on one line):

mvn archetype:create
   -DgroupId=com.burlesontech.sw
   -DartifactId=webapp
   -DarchetypeArtifactId=maven-archetype-webapp

You project will now have the following structure:

  • sw
    • webapp
      • src
        • main
          • resources
          • webapp
            • WEB-INF
              • web.xml
            • index.jsp
      • pom.xml
    • .project

In Eclipse, right-click on the project and select Refresh to see this stuff within Eclipse. (Note that the .project file will typically not appear within Eclipse, but it is there.)

Back to Top

Step Three - Define Your Tomcat Server in Maven Settings

Open your Maven settings.xml file (e.g. C:"Documents and Settings"Administrator".m2"settings.xml) and add a server 'myserver' with the credentials for logging into the Tomcat Manager application:

<settings>
    <servers>
    <server>
        <id>myserver</id>
        <username>admin</username>
        <password>admin</password>
    </server>
    </servers>
...
Back to Top

Step Four - Point Your Pom to Your Tomcat Server

Open the pom.xml file in the 'sw' project and replace the <build> section so that it looks like this:

<build>
    <finalName>sw</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>tomcat-maven-plugin</artifactId>
            <configuration>
                     <server>myserver</server>
                     <path>/sw</path>
            </configuration>
        </plugin>
    </plugins>
</build>

Here, we have added the Tomcat plugin for Maven. Note that the <configuration> section needs to point to the server you defined in settings.xml ('myserver'). The <finalName> and the <path> are used to tell the web context that you want to deploy to. In this case, we'll be able to access our application at http://localhost:8080/sw.

Back to Top

Step Five - Build and Deploy the Web App

On a command prompt, you can now cd into the sw/webapp directory where the project pom exists. The execute the following command:

mvn tomcat:deploy

If you get a BUILD SUCCESSFUL message, you should then be able to access your web application at http://localhost:8080/sw/.

You should see "Hello World!" in your web browser (from the index.jsp page). Now you're all set and ready to start building the next killer web app!

If you try to deploy again with the same command, you will likely get a FAIL message because the application already exists at the path. For successive deployments, use the following command instead:

mvn tomcat:redeploy

Next, you may want to enable log4j logging for your new application; check out How to setup Log4j in a web app - fast.



seal 2010-01-08 13:46 发表评论
]]>
eclipse里面理Maven目http://www.aygfsteel.com/sealyu/archive/2010/01/04/308168.htmlsealsealMon, 04 Jan 2010 05:51:00 GMThttp://www.aygfsteel.com/sealyu/archive/2010/01/04/308168.htmlhttp://www.aygfsteel.com/sealyu/comments/308168.htmlhttp://www.aygfsteel.com/sealyu/archive/2010/01/04/308168.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/308168.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/308168.html q时候类库会报错Q?Unbound classpath variable: 'M2_REPO/**/***/***.jar'

q是因ؓeclipse里面q没配置对应的classpath variable?br /> 配置Ҏ(gu)如下Q?br />

Eclipse->Windows->Preferences->java->Build Path->Classpath Variable->New

名字:M2_REPOQ属性选folderQ指向C:\Documents and Settings\your name\.m2\repository





seal 2010-01-04 13:51 发表评论
]]>
Maven Standard Directory Layouthttp://www.aygfsteel.com/sealyu/archive/2009/12/31/307905.htmlsealsealThu, 31 Dec 2009 06:00:00 GMThttp://www.aygfsteel.com/sealyu/archive/2009/12/31/307905.htmlhttp://www.aygfsteel.com/sealyu/comments/307905.htmlhttp://www.aygfsteel.com/sealyu/archive/2009/12/31/307905.html#Feedback1http://www.aygfsteel.com/sealyu/comments/commentRss/307905.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/307905.htmlHaving a common directory layout would allow for users familiar with one Maven project to immediately feel at home in another Maven project. The advantages are analogous to adopting a site-wide look-and-feel.

The next section documents the directory layout expected by Maven and the directory layout created by Maven. Please try to conform to this structure as much as possible; however, if you can't these settings can be overridden via the project descriptor.

src/main/java Application/Library sources
src/main/resources Application/Library resources
src/main/filters Resource filter files
src/main/assembly Assembly descriptors
src/main/config Configuration files
src/main/webapp Web application sources
src/test/java Test sources
src/test/resources Test resources
src/test/filters Test resource filter files
src/site Site
LICENSE.txt Project's license
README.txt Project's readme

At the top level files descriptive of the project: a pom.xml file (and any properties, maven.xml or build.xml if using Ant). In addition, there are textual documents meant for the user to be able to read immediately on receiving the source: README.txt, LICENSE.txt, etc.

There are just two subdirectories of this structure: src and target. The only other directories that would be expected here are metadata like CVS or .svn, and any subprojects in a multiproject build (each of which would be laid out as above).

The target directory is used to house all output of the build.

The src directory contains all of the source material for building the project, its site and so on. It contains a subdirectory for each type: main for the main build artifact, test for the unit test code and resources, site and so on.

Within artifact producing source directories (ie. main and test), there is one directory for the language java (under which the normal package hierarchy exists), and one for resources (the structure which is copied to the target classpath given the default resource definition).

If there are other contributing sources to the artifact build, they would be under other subdirectories: for example src/main/antlr would contain Antlr grammar definition files.



seal 2009-12-31 14:00 发表评论
]]>
Maven的多模块(Multi-Module)工程的pom~写(?http://www.aygfsteel.com/sealyu/archive/2009/12/30/307760.htmlsealsealWed, 30 Dec 2009 06:48:00 GMThttp://www.aygfsteel.com/sealyu/archive/2009/12/30/307760.htmlhttp://www.aygfsteel.com/sealyu/comments/307760.htmlhttp://www.aygfsteel.com/sealyu/archive/2009/12/30/307760.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/307760.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/307760.html

对于使用maven的骨架创建工E,惛_大家都已l熟(zhn)了Q这里是一些常用的工程cdQ如想看到更多的骨架可以使用mvn的交互式Interactive generate Goal创徏指oQmvn archetype:generate


// Creating a simple java application
mvn archetype:create -DgroupId=[your project's group id] -DartifactId=[your project's artifact id]

// Creating a webapp
mvn archetype:create -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeArtifactId=maven-archetype-webapp

// Creating a site
mvn archetype:create -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeArtifactId=maven-archetype-site-simple

// Creating a mojo
mvn archetype:create -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeArtifactId=maven-archetype-mojo

// Creating a portlet
mvn archetype:create -DgroupId=[your project's group id] -DartifactId=[your project's artifact id] -DarchetypeArtifactId=maven-archetype-portlet

现在想介l的是多模块的工E的构徏?/p>

典型的多模块划分Q即按MVC的分层方式来构徏多个模块Q如工程包括web,business,core3个模块。好我们先看看主工程的pom中应d些什么,h意红色文字部?/p>


1.dE的pom文g中内容:

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.your-company.xxxx</groupId>
    <artifactId>xxxx</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>
    <name>xxxx Project</name>
    <url>http://maven.apache.org</url>

    <!-- 工程所包含的模?-->

    <modules>
        <module>xxxx-core</module>
        <module>xxxx-business</module>
        <module>xxxx-web</module>
    </modules>

2.Web模块的pom文gQ?/strong>

    <!-- 父的pom文g位置 -->

    <parent>
        <groupId>com.your-company.xxxx</groupId>
        <artifactId>xxxx</artifactId>
        <version>1.0</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.your-company.xxxx</groupId>
    <artifactId>xxxx-web</artifactId>
    <packaging>war</packaging>
    <version>1.0</version>
    <name>xxxx-web/name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <!-- Application Dependencies -->
        <!-- Web层所依赖的上两层模块 -->

        <dependency>
            <groupId>com.your-company.xxxx</groupId>
            <artifactId>xxxx-core</artifactId>
            <version>${version}</version>
        </dependency>
        <dependency>
            <groupId>com.your-company.xxxx</groupId>
            <artifactId>xxxx-business</artifactId>
            <version>${version}</version>
        </dependency>

        ...

    <dependencies>


3.完成后,mvn eclipse:eclipse后的文g目录为:

xxxx

├─xxxx-core

? ├─pom.xml
? ├─.settings
? ├─src
? ? ├─main
? ? ? ├─java
? ? ? └─resources
? ? └─test
? ?nbsp;     ├─java
? ?nbsp;     └─resources
? └─target
?nbsp;     ├─classes
?nbsp;     └─test-classes

├─xxxx-business
? ├─pom.xml
? ├─.settings
? ├─src
? ? ├─main
? ? ? ├─java
? ? ? └─resources
? ? └─test
? ?nbsp;     ├─java
? ?nbsp;     └─resources
? └─target
?nbsp;     ├─classes
?nbsp;     └─test-classes

├─xxxx-web

? ├─pom.xml
? ├─.settings
? ├─src
? ? ├─main
? ? ? ├─java
? ? ? └─resources
? ? └─test
? ?nbsp;     ├─java
? ?nbsp;     └─resources
? └─target
?nbsp;     ├─classes
?nbsp;     └─test-classes

|--pox.xml



seal 2009-12-30 14:48 发表评论
]]>
Maven 2.0:~译、测试、部|Ӏ运行(转)http://www.aygfsteel.com/sealyu/archive/2009/12/30/307750.htmlsealsealWed, 30 Dec 2009 05:52:00 GMThttp://www.aygfsteel.com/sealyu/archive/2009/12/30/307750.htmlhttp://www.aygfsteel.com/sealyu/comments/307750.htmlhttp://www.aygfsteel.com/sealyu/archive/2009/12/30/307750.html#Feedback0http://www.aygfsteel.com/sealyu/comments/commentRss/307750.htmlhttp://www.aygfsteel.com/sealyu/services/trackbacks/307750.html

摘要:

Maven1.0 已经历了几年的时_q且作ؓAnt的替代品已被q大的开发h员所接收Q但它ƈ没有在很大程度开发h员从Ant的build.xml文g中解脱出来? Maven1.0速度慢ƈ且笨拙,使用h的困隑ֺq不亚于使用Ant的项目。事实上Q它的核心是ZAnt实现的。在l过了几乎彻底的重写 后,Maven2.0诞生了?
一 个Java目中最困难的地方就是如何着手启动它。在启动目之前Q我们必配|好所有的逻辑关系。比如,Java源代码应该放在何处?单元试应该在何 处进行?依赖的jar包应该如何放|?如何构徏目Q如何Ş成文档,如何试和部|项目?在这U情况下Q开发h员不同的处理选择会影响目的余下部分? (zhn)的选择可能会你陷入困境,也可能会在将来证明?zhn)是一位Java架构大师。我们假定后者是我们奋斗的目标,接下来就q入我们的正题?br />
? Z个Java目可以使用很多工具Q其中包括Ant。Ant作ؓ一Ƒօ有革命性的工具Q一直是众多开发者用工具中的首选,它能使开发h员摆׃用大? make命o的苦对于那些不太熟(zhn)make命o的h来说Q他们有充的理由来表明使用命oq不是构建Java目的最好工P因ؓ它不具备q_独立? q且不易使用。Ant的出现解决了以上的问题,它用了一个^台独立的工具Q该工具能够解析XML配置文gQ即build.xml。虽然Ant׃其诸? 的优点而备受欢q,但它同样有一些缺炏Vbuild.xml文g׃采用了极其简短的描述方式Q得开发h员需要预先学习它的语法。虽然学习曲U不是很? 峭,但Java开发h员更应该把时间放在开发上面?br />
Maven是新一代的生力军,它的境遇正好和几q前的Ant十分cM? Maven1.0已经历了几年的时_q且作ؓAnt的替代品已被q大的开发h员所接收Q但它ƈ没有在很大程度开发h员从Ant的build.xml? 件中解脱出来。Maven1.0速度慢ƈ且笨拙,使用h的困隑ֺq不亚于使用Ant的项目。事实上Q它的核心是ZAnt实现的。在l过了几乎彻底的? 写后QMaven2.0诞生了?br />
Maven2.0的优?/span>

Maven2.0有许多很好功能,q些功能不仅仅是帮助(zhn)构建项目。如果?zhn)刚刚开始启动一个Java目Qƈ且想使该目快速地开展下去,Maven2.0能够在几分钟内达到?zhn)的要求。以下是Maven2.0的一些优点:
--标准的项目布局和项目结构生成器
--标准的依赖管理机?br /> --多项目支?br /> --在开发者需要的时候及时地下蝲新的插g和功能部?br /> --生成最新项目信息的|站
--集成了源代码控制软gQCVS和Subversion

以上列表展示的只是Maven2.0特点中的一部分。但q以Maven2.0成ؓ一个构建管理系l可靠的选择。既然我们已l知道Maven是个什么东西了Q接下来让我们看看如何用它?br />
入门

? 们要做的W一件事情就是设|目录结构,但这q不需要让我们手动讄QMaven会根据?zhn)开发的目cd来ؓ(zhn)做qg事。一旦?zhn)下蝲q解压了最新发布的 Maven 2.0Q?zhn)应该Maven所在目录下面的bin目录d到?zhn)的系l\径下。?zhn)可以q行命omvn -version来测试?zhn)的安装?br />
既然已经安装上了工具Q让我们看看创徏一个简单的Java目的例子。Maven使用原型来决定目录结构是如何展现的。Maven自带了几个内建的原型Q?zhn)也可以自定义原型?br />
mvn archetype:create -DgroupId=com.oreilly -DartifactId=my-app

(zhn)看Q这q成了我们的项目布局?br /> my-app
----src
    ----main
        ----java
           ----com
               ----oreilly
    ----test
        ----java
            ----com
                ----oreilly

对, p么简单。这U目录结构可以通过创徏一个新的原型来覆写Q但q不推荐q么做,因ؓMaven的一个优点就是用标准的目录l构。该目录l构包含两个源代 码树Q一个是Java应用E序的源代码Q另一个是单元试代码。同时?zhn)也许会注意到Q当W一ơ运行Maven的时候,它会q行一些下载工作。当(zhn)开始调? 工具ӞMaven会根据?zhn)使用的插件来更新自n的一些所需功能。Maven默认会从Ibiblio存储库中得到更新。?zhn)可以在Maven安装目录下的 conf目录中,或者项目自w中修改Mavenq程存储库的选择?br /> (zhn)会发现Maven在my-app目录下创Z一个pom.xml文g。这是项 目的最基本部分。pom.xml文g包含了一l指令,q些指o告诉Maven如何构徏目和包含哪些其它的Ҏ(gu)指oQPOM?#8220;目对象模型”的羃写)? 在默认的情况下,Maven包含了JUnit的依赖以此来鼓励单元试?br />
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.oreilly</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


创徏完项目后Q我们可以往目里添加代码ƈ使用Maven的所有全新技巧。注意以下命令必dpom.xml文g所在的目录中运行?br /> --mvn testQ运行应用程序中的单元测?br /> --mvn packageQ依据项目生成jar文g
--mvn installQ将目的jar文gd到库中,        以备依赖此项目时使用
--mvn siteQ生成项目相关信息的|站
--mvn cleanQ清除目标目录中的生成结?br /> --mvn eclipse:eclipseQ生成Eclipse目文g

接下来我们看看稍微复杂点的地方,我们知道手动开始一个Java web目比手动开始一个简单的Java目更耗时Q然而Maven的用则能化难ؓ易。下面的例子Q实际上是一行命令)展现了项目结构的构造?br /> mvn archetype:create -DgroupId=com.oreilly
    -DartifactId=Oreilly
    -DarchetypeArtifactId=maven-archetype-webapp


生成的结果结构如下所C:
Oreilly
----src
    ----main
        ----resources
        ----webapp
            ----WEB-INF

q? 一ơ,我们的项目由于支持了包含在war文g中的web资源而设|有所不同。pom.xml文g中将包含一行来表明目应该被打包成war? Ӟ<packaging>war</packaging>。现在就可以使用mvn package命o来生成war文g。不用担心如何从WEB-INF/lib目录中得C赖项Q在依赖属性D讄成compile的情况下QMaven 会自动包含依赖项。也可以以下代码添加到pom.xml文g中来改变war文g的名Uͼ
<build>
    <finalName>PromoteC</finalName>
</build>


依赖理

? 建好目l构Q添加完一些代码,试q编译好应用E序后,接下来可以看看Maven是如何处理依赖关pȝ。ؓ了给目d一个依赖项Q必d此依赖项d 到pom.xml文g中。下ơ运行Maven的时候,它将从Ibiblio存储库中得到q个依赖,q且此依赖Ҏ(gu)加到目构徏路径中?br />
? 于依赖的问题有几个重要的事情值得注意。在写这文章的时候,Maven中最大的ȝ之处是不能从Maven存储库中获取Sun的jar文g。这个问? 归因于Sun在其代码中设|的许可证限制。解册个问题的办法有两U,一U是下蝲q些代码q将它们安装在?zhn)本地的存储库中,另一U是做一个外部声明,q将 q个声明指向文gpȝ中依赖项所在的位置。希望Sun能够早地创q存储库,管如此QMaven也会被升U来使之能够下蝲q些资源Q只是在下蝲? 前它会提C用h受许可证协议?br />
另外一个麻烦的地方是有时候用的最新的库文件可能在q程存储库中不存在。另一U可能是׃无法讉K InternetQ需要所有的依赖w能在本地获取。这些问题的最好解x案就是将jar文g安装到本地的存储库中。将本地的存储库攑֜一台web服务? 上也同样是个便利之DQ这h个开发团队就能从此获益,每个人都没有必要ȝ理自q存储库了。改变Maven的存储库路径只需单地~辑其安装目录下 conf文g夹下面的settings.xml文g卛_?br />
在Maven中用依赖是单的。让我们看看往上述pom.xml文g中添加一 个依赖项的情c我们虽然已l用了JUnitQ但让我们将功能强大的Quartz库添加到目中。Quartz是一ƄUJava~写的关于时间安排的 开源项目,它是(zhn)时间安排需求方面的很好的选择?br />
<dependency>
   <groupId>quartz</groupId>
   <artifactId>quartz</artifactId>
   <version>1.5.1</version>
   <scope>compile</scope>
</dependency>


? 们仅仅只需d<dependencies>q个元素QMavenp下蝲Quartzq将其作为项目中的一个依赖项。不用担心Quartz 的依赖项Q一个Maven的存储库包含依赖项自n依赖的资源信息,当Maven下蝲Quartz的时候,它自w的依赖资源也同样会被下载。ؓ了验证版? ?.5.1的Quartz存在于Ibiblio库中Q我们可以浏览Maven存储库。注意到scope参数的用,它告诉了Maven依赖在何种阶段 是所需的。在使用JUnit的情况下Q我们设|scope参数的gؓtest来告诉Mavenq个依赖只是在试阶段所需的,而不是运行时所需的资源? 以下是scope参数值的说明Q?br /> --compileQ默认倹{表明是所有Q务所需的资?br /> --testQ运行所有的试用例时所需资源
--runtimeQ表明是q行时所需资源
--providedQJDK部分或应用服务器的classpath所需的资?br />
? 在,如何处理那些ȝ的Sun的jar包和那些需要但却不能在q程存储库中扑ֈ的jar包了Q我们必M用Maven来手动将q些jar包安装到本地的存 储库中。不用担心,q没有听上去那么困难。ؓ了做个示例,我们安装Java Activation框架的jar包。首先我们必MSun的站点上下蝲此jar包,接着我们使用Maven它导入本地的存储库中。?zhn)自己也可以按? Maven上传资源指南中的指导缺的jar包安装到Ibiblio中?br /> mvn install:install-file -Dfile=activation.jar
    -DgroupId=javax.activation -DartifactId=activation
    -Dversion=1.0 -Dpackaging=jar


? 在,新的jar包就像其它的目依赖一样安装到了本地存储库中。在只需d依赖声明后,我们已准备qA了。在djar包和声明它们Z赖项Ӟ必须 保版本信息的正性。版本的不匹配会DMaven在寻找资源时的失败。在导入Sun的jar包时Q如果?zhn)需要寻求标准命名参数的帮助Q可以参考Sun 标准jar包命名。记住,在目前?zhn)不能通过存储库来公开发布q些jar包,q将q反Sun的用条ƾ?br />
<dependency>
   <groupId>javax.activation</groupId>
   <artifactId>activation</artifactId>
   <version>1.0</version>
   <scope>compile</scope>
</dependency>


(zhn)? 或许惛_依赖存入一个源代码控制器的库中Q源代码控制器决不能执行q个d。依赖项是经常变化的Qƈ且通常有一套数字方案来标明其版本。这是_(zhn)明 地希望有一个内部远E存储库的备份,如果(zhn)有一个,q将保在存储库服务器崩溃ƈ且不能恢复的情况下,(zhn)不会丢失所有的自定义资源。不依赖项攑օ源代 码控制器中也会节省源代码控制器的存储库服务器上的大量盘I间?br />
配置存储?/span>

? 求项目的每个开发者必dconf目录中配|存储库是不方便的,所以Maven可以同时查看多个存储库ƈ且将它们全部配置在pom.xml文g中。让我们 看看一个例子,它展CZ如何在应用程序用使用多个存储库。在以下从pom.xml文g摘录的片断中Q我们设|了两个存储库来让MavenL依赖V? Ibiblio一直是默认的存储库Q我们又d了Planet Mirror作ؓ后援存储库。我们也可以让团队用的本地web服务器作为第二个存储库?br />
<repositories>
    <repository>
      <id>Ibiblio</id>
      <name>Ibiblio</name>
      <url>http://www.ibiblio.org/maven/</url>
    </repository>
    <repository>
      <id>PlanetMirror</id>
      <name>Planet Mirror</name>
      <url>http://public.planetmirror.com/pub/maven/</url>
    </repository>
  </repositories>


使用pom.xml父文件来构徏多个目

? 件公叔R常的一U做法就是将多个目构徏C要品中。维护依赖关p链和一ơ性地构徏整个产品以成ؓ一个挑战,但是如果使用Maven的话Q事情将变得 单。如果?zhn)创徏了一个指向其它子模块的pom.xml父文ӞMavenؓ(zhn)处理整个构E。它?yu)分析每个子模块的pom.xml文gQƈ且按照这 些子模块的相互依赖顺序来构徏目。如果每个项目明地指明它们的依赖项Q那么子模块在父文g中的攄序是不造成M影响的。但是考虑到其他的开发者, 最好保证子模块在pom.xml父文件中的放|顺序和(zhn)期望的子项目被构徏的顺序一栗下面我们看个示例?br /> pom.xmlL件如下:
<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.oreilly</groupId>
  <version>1.0-SNAPSHOT</version>
  <artifactId>my-app</artifactId>
  <packaging>pom</packaging>
  <modules>
    <module>Common</module>
    <module>Utilities</module>
    <module>Application</module>
     <module>WebApplication</module>
  </modules>
</project>


? 们需要确保WebApplication子模块包含了所有的三个jar包,所以需要将q些jar包声明ؓ依赖V在q个例子中,Utilities目? 赖于Common目Q所以Utilities目中需要添加一个对Common目的依赖。Application子模块也是同L道理Q因为它依赖? Common和Utilities目QUtilities又赖于Common。如果这个例子中?0个子模块Qƈ且它们都怺依赖Q这会得新开发者难 以算Z么项目依赖于其它目Q所以这正好是要求确保pom.xml父文件中目攄序要清除的原因?br />
以下是Utility模块的依赖项Q?br />
<dependencies>
    <dependency>
      <groupId>com.oreilly</groupId>
      <artifactId>Common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>


以下是如何声明Application模块的依赖项Q?br />
<dependencies>
    <dependency>
      <groupId>com.oreilly</groupId>
      <artifactId>Common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.oreilly</groupId>
      <artifactId>Utilities</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
  </dependencies>


最后是WebApplication模块的依赖项Q?br />
<dependencies>
    <dependency>
      <groupId>com.oreilly</groupId>
      <artifactId>Common</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
      <groupId>com.oreilly</groupId>
      <artifactId>Utilities</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    
    <dependency>
      <groupId>com.oreilly</groupId>
      <artifactId>Application</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency>
    
</dependencies>


现在Q我们只需为每个子模块的pom.xml文gd一个元素来表明它们是一个逻辑构徏的一部分Q?br />
<parent>
    <groupId>com.oreilly</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>


在pom.xml父文件所在的同一个目录中Q存在有目目录QCommon, Utilities, Application, 和WebApplication。当我们在该目录中运行mvn package命oӞq些目会按照依赖顺序而被构徏?br />
插g和报?/span>

Maven2.0 有大量的插g可以使用。不q的是,׃Maven的重写,Maven1.0的插件不能在2.0中用。尽如此,q是存在一些可以用的Maven2.0 的插件。下面pom.xml文g中的插g配置CZ是直接从Maven2.0|站上得来的。这个插件是用来配置~译选项的?br />
<plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>1.5</source>
        <target>1.5</target>
      </configuration>
    </plugin>
  </plugins>


Maven报表插g可以用来生成不同的报表,q些报表是在当你使用mvn site命o生成目的站Ҏ(gu)产生的。下面的例子展示了如何?lt;reporting>元素来配|这cL件中的一个?br />
<reporting>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-project-info-reports-plugin</artifactId>
      </plugin>
    </plugins>
  </reporting>


Maven Plugin Matrix是一个十分实用的工具Q它能给出哪些Maven插g适合于哪些版本的Maven?br />
Maven ?Eclipse

? 何能使全世界最好的IDE变得更好了?{案是用Maven2的插Ӟ它能帮助(zhn)寻找依赖项q自动地它们添加到pom.xml文g中。虽然最好的Ҏ(gu)? 首先使用Maven来创建?zhn)的项目,然后再用命omvn eclipse:eclipse来生成Eclipse目文gQ这h初就能得C个好的目录结构,但Maven也可通过其Eclipse插g来管理Q? 目?br />
(zhn)可以在Eclipse自n的升U器中输入站点http://m2eclipse.codehaus.org/来安装插件。在安装 完成q启IDE后,(zhn)需要在Eclipse的参数选项中配|此插gQ设|本地存储库的位|。这是很重要的一步,因ؓ如果Eclipse默认的存储库不能 匚w(zhn)默认的需求,Maven会重C载?zhn)的依赖项。配|完成后Q将目导入EclipseQ鼠标右击该目Q选择Maven 2 -> Enable。现在?zhn)可以回到之前的步骤,?zhn)可以有更多的选项比如d依赖,q将弹出一个搜索框Q?zhn)可以搜烦依赖ƈd它们Q插件会替你~辑 pom.xml文g?br />
插g会用Maven来构建?zhn)的项目,像Eclipse使用Ant来构建项目一栗如果?zhn)惌取更多的关于Eclipse整合Maven的信息,可以查阅Maven站点上的Eclipse集成Maven 2.x使用指南?br />
? 一斚wQ如果?zhn)是一个IntelliJ使用爱好者,(zhn)也能通过q行指omvn idea:idea来完成同Ld。这些IDE        工具能够节省开发h员的旉。例如,如果一个开发h员ؓ一个项目添加了一些特征,团队? 的其他开发h员只需从源代码控制器的存储库中重新获取目文g卛_Q这p省了每个开发h员必配|IDE的时间?br />
l论

Maven2.0 有着许多实用的特点,q且完成d十分。Maven中最值得U赞的地方就是用了标准的目录结构和部v。这׃得开发h员能够适应不同的项目,q且? 用学习Q何结构方面新的东西,也不用掌握特D的指o来构建结构。Maven可以通过U脚本来实现。在文档斚wQ由于用了目站点构徏工具Q所以当目? 建完成后Q?zhn)可以查看所有开发的当前状态?br />
毫无疑问Q当提到构徏配置的׾~性,易用性和目理斚wӞMaven2.0_以将Ant 取而代之。在接下来的几年内,我们看到Maven作ؓ标准构徏技术更多的使用Q直到有人带来了大家公认?#8220;高捕鼠?#8221;。?zhn)可以从下面列出的Maven 目站点上下载Maven?br />
资源
Matrix:http://www.matrix.org.cn/
Onjava:http://www.onjava.com/
Maven目站点

Chris Hardin 是McLeod软g公司的高UJava工程师?/strong>


seal 2009-12-30 13:52 发表评论
]]>
վ֩ģ壺 | | Ϫ| пǰ| | ˮ| | | | | | Ҧ| | | | | ְ| ̨| | | ˮ| | IJ| ̨| | | | ƾ| ϻ| ͨμ| | Ҧ| ϲ| | | | ݳ| | ɽ| ֥| ߶|