hello maven

创建项目

mvn archetype:create -DgroupId=org.jee.tool -DartifactId=jee-tool

创建org.jee.tool项目

 

如果报错提示archetype没找到,解决办法如下:

 

I kept getting an error about the maven-archetype-plugin whenever I tried to create a new application using maven 2. It turns out part of my local maven repository must have become corrupted or failed to download properly. It was solved by deleting .m2/repository/org/apache/maven and allowing maven to download all the required dependencies again.

 

创建子模块项目

cd org.jee.tool

mvn archetype:create -DgroupId=org.jee.tool -DartifactId=jee-local-cache

mvn archetype:create -DgroupId=org.jee.tool -DartifactId=jee-log

mvn archetype:create -DgroupId=org.jee.tool -DartifactId=jee-log-analyse

mvn archetype:create -DgroupId=org.jee.tool -DartifactId=jee-rpc

 

此时查看jee-tool/pom.xml,已经自动添加了<modules>的4个子项目模块。

 

创建eclipse项目文件

mvn eclipse:eclipse 生成eclipse所需文件

mvn eclipse:clean 删除eclipse配置文件

更多eclipse插件:http://maven.apache.org/plugins/maven-eclipse-plugin/

 

 

config plugin

plugin 类型:build plugin & report plugin

url:http://maven.apache.org/guides/mini/guide-configuring-plugins.html

 

插件编写:

 

/**
 * @goal query
 */
public class MyQueryMojo
    extends AbstractMojo
{
    /**
     * @parameter expression="${query.url}"
     */
    private String url;

    /**
     * @parameter default-value="60"
     */
    private int timeout;

    /**
     * @parameter
     */
    private String[] options;

    public void execute()
        throws MojoExecutionException
    {
        ...
    }
}

 

通过 <configuration/>配置参数:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-myquery-plugin</artifactId>
        <version>1.0</version>
        <configuration>
          <url>http://www.foobar.com/query</url>
          <timeout>10</timeout>
          <options>
            <option>one</option>
            <option>two</option>
            <option>three</option>
          </options>
        </configuration>
      </plugin>
    </plugins>
  </build>
  ...
</project>

 

通过参数配置

mvn myquery:query -Dquery.url=http://maven.apache.org

 

可以配置Collection,Class,Properties

 

 

使用executions能将插件插入到不同的build阶段

You can also configure a mojo using the <executions> tag. This is most commonly used for mojos that are intended to participate in some phases of the build lifecycle . Using MyQueryMojo as an example, you may have something that will look like:

<build>
    <plugins>
      <plugin>
        ...
        <executions>
          <execution>
            <id>execution1</id>
            <phase>test</phase>
            ...
          </execution>
          <execution>
            <id>execution2</id>
            <phase>install</phase>
            <configuration>
              <url>http://www.bar.com/query</url>
              <timeout>15</timeout>
              <options>
                <option>four</option>
                <option>five</option>
                <option>six</option>
              </options>
            </configuration>
            <goals>
              <goal>query</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

 

 

build lifecyle

  • validate - validate the project is correct and all necessary information is available
  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package - take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test - process and deploy the package if necessary into an environment where integration tests can be run
  • verify - run any checks to verify the package is valid and meets quality criteria
  • install - install the package into the local repository, for use as a dependency in other projects locally
  • deploy - done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

phase and goal

mvn clean dependency:copy-dependencies 

phase = clean

goal = dependency:copy-dependencies

 

结合plugin在不同周期执行ant命令:在validate阶段删除target下全部文件

<build>
		<plugins>
			<plugin>
				<artifactId>maven-antrun-plugin</artifactId>
				<executions>
					<execution>
						<phase>validate</phase>
						<goals>
							<goal>run</goal>
						</goals>
						<configuration>
							<tasks>
								<delete dir="${project.basedir}/target" />
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
</plugins></build>

 

 

 

Building For Different Environments with Maven 2

使用antrun-plugin插件行进文件删除以及拷贝

 

以下profile id为deploy177,通过mvn install -Pdeploy177进行打包

 

 <profiles>
   <profile>
     <id>deploy177</id>
     <build>
       <plugins>
         <plugin>
           <artifactId>maven-antrun-plugin</artifactId>
           <executions>
             <execution>
               <phase>test</phase>
               <goals>
                 <goal>run</goal>
               </goals>
               <configuration>
                 <tasks>
                   <delete file="${project.build.outputDirectory}/environment.properties"/>
                   <copy file="src/main/resources/environment.test.properties"
                         tofile="${project.build.outputDirectory}/environment.properties"/>
                 </tasks>
               </configuration>
             </execution>
           </executions>
         </plugin>
         <plugin>
           <artifactId>maven-surefire-plugin</artifactId>
           <configuration>
             <skip>true</skip>
           </configuration>
         </plugin>
         <plugin>
           <artifactId>maven-jar-plugin</artifactId>
           <executions>
             <execution>
               <phase>package</phase>
               <goals>
                 <goal>jar</goal>
               </goals>
               <configuration>
                 <classifier>deploy177</classifier>
               </configuration>
             </execution>
           </executions>
         </plugin>
       </plugins>
     </build>
   </profile>

   .. Other profiles go here ..

 </profiles>

打包方式是war包的配置:

                                        <plugin>
						<artifactId>maven-war-plugin</artifactId>
						<executions>
							<execution>
								<phase>package</phase>
								<goals>
									<goal>war</goal>
								</goals>
								<configuration>
									<classifier>deploy177</classifier>
								</configuration>
							</execution>
						</executions>
					</plugin>

 

 

 

build war

见http://maven.apache.org/plugins/maven-war-plugin/usage.html

使用war plugin:

<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-war-plugin</artifactId>
				<version>2.0</version>
				<configuration>
					<webResources>
						<resource>
							<!--
								fund-web/fund-web-war/ this is relative to the pom.xml directory
							-->
							<directory>fund-web/fund-web-war/WebRoot</directory>
						</resource>
					</webResources>
				</configuration>
			</plugin>

 

 

结合yuicompressor压缩js和css

这段代码调试了很久,在maven里面执行总是出错。没有源码不好排查。最终是功能是实现了。但是这里的srcfile和targetfile是同一个文件,通过拷贝到overlay的war中进行修改。

<apply executable="java" parallel="false" failοnerrοr="true"
	dest="${project.basedir}/WebRoot" append="false" force="true">
	<fileset dir="${project.basedir}/WebRoot">
		<include name="**/**.js" />
		<include name="**/**.css" />
	</fileset>
	<arg line="-jar" />
	<arg path="${project.basedir}/../lib/yuicompressor-2.4.2.jar" />
	<arg line="--charset utf-8" />
	<srcfile />
	<arg line="-o" />
	<mapper type="glob" from="*" to="*" />
	<targetfile />
</apply>

 

maven使用ant task的apply时有bug,以上配置跑出来结果srcfile和targetfile均在最后出现。 

 

 

使用overlay将不同文件打包

通过profile进行配置文件修改主要针对编译后的class的。无法满足压缩js和css的替换。

期待的打包方式是:开发人员开发css,js,本地运行无压缩版本。

打包时自动将css,js压缩打包。

 

maven提供overlay进行打包。

地址:http://maven.apache.org/plugins/maven-war-plugin/overlays.html

 

这个文档看了基本上一头雾水。

我花费了3个小时,终于把这个overlay实现了。请看此图:

overlay不是在自己的项目里面进行替换,而是需要新建一个pom,

 

依赖原有war

	<dependencies>
		<dependency>
			<groupId>com.tenpay.fund.web</groupId>
			<artifactId>fund-web-war</artifactId>
			<version>1.0</version>
			<classifier>deploy177</classifier>
			<type>war</type>
			<scope>runtime</scope>
		</dependency>
	</dependencies>

 

设置哪些需要include,哪些需要exclude:这是表示当前项目的文件是否覆盖原有的war内容

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-war-plugin</artifactId>
	<version>2.0</version>
	<configuration>
		<webResources>
			<resource>
				<directory>${project.basedir}/WebRoot</directory>
			</resource>
		</webResources>
		<warName>fund</warName>
		<dependentWarExcludes>**/*.js</dependentWarExcludes>
		<overlays>
			<overlay>
				<groupId>com.tenpay.fund.web</groupId>
				<artifactId>fund-web-war</artifactId>
				<classifier>deploy177</classifier>
				<excludes>
					<exclude>test.html</exclude>
				</excludes>
			</overlay>
		</overlays>
	</configuration>
</plugin>

 


 

通过tomcat-maven-plugin部署到服务器tomcat

1.配置maven\conf\settings.xml,在servers内添加测试服务器manager用户名密码

  <servers>
        <server>
          <id>f3_server</id>
          <username>admin</username>
          <password>admin</password>
        </server>
  </servers>

 



2.在war项目的pom文件添加tomcat-maven插件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>tomcat-maven-plugin</artifactId>
    <configuration>
        <server>f3_server</server>
        <url>http://f3.tenpay.com/manager/html</url>
        <warFile>${project.basedir}/target/fund.war</warFile>
        <path>/fund</path>
    </configuration>
</plugin>

 


server对应setting.xml内配置
url对应manager的html
warFile对应生成的war文件
path对应部署的应用路径

3.运行tomcat插件
tomcat中不存在应用时运行mvn tomcat:deploy
tomcat中存在应用时运行mvn tomcat:redeploy



附录:
执行redeploy的部分输出:
[INFO] [war:war]
[INFO] Exploding webapp...
[INFO] Copy webapp webResources to D:\workspace\trunk_3\fund\fund-web\fund-w
[INFO] Copy webapp webResources to D:\workspace\trunk_3\fund\fund-web\fund-w
[INFO] Assembling webapp fund-war-compress in D:\workspace\trunk_3\fund\fund
[INFO] Expanding: D:\m2\repository\com\tenpay\fund\web\fund-web-war\1.0\fund
eb\fund-war-compress\target\war\work\fund-web-war-1.0
[INFO] Overlaying 1 war(s).
[INFO] Generating war D:\workspace\trunk_3\fund\fund-web\fund-war-compress\t
[INFO] Building war: D:\workspace\trunk_3\fund\fund-web\fund-war-compress\ta
[INFO] [tomcat:redeploy]
[INFO] Deploying war to http://f3.tenpay.com/fund
[INFO] OK - Undeployed application at context path /fund
[INFO] OK - Deployed application at context path /fund
[INFO] ---------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL

 

本地构建远程资源中心

 

	<distributionManagement>
		<repository>
			<id>tenpay-releases</id>
			<name>tenpay Release Repository</name>
			<url>http://10.6.208.112/nexus/content/repositories/releases/</url>
		</repository>
		<snapshotRepository>
			<id>tenpay-snapshots</id>
			<name>tenpay Snapshot Repository</name>
			<url>http://10.6.208.112/nexus/content/repositories/snapshots/</url>
		</snapshotRepository>
	</distributionManagement>
 

 

通过distributionManagement设置

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值