maven引入学习

http://www.cnblogs.com/hanwesley/articles/1948230.html

Maven简单介绍
Maven是基于项目对象模型(Project Object Model),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具.
掌握maven就是要学会如何编写pom.xml,尤其对于多个子项目的管理
约定俗成
src/main/java  存放Java文件
src/main/resources  存放资源文件
src/main/test  存放测试文件
Maven坐标
<groupId>com.ibatis</groupId>  项目属于哪个组
<artifactId>ibatis-sqlmap</artifactId>  项目在组中唯一的ID
<version>2.1.0.565</version>  项目当前的版本
<name>ibatis sqlmap包</name>  非必需,描述信息
在Maven中任何的jar,POM,或者war都是基于坐标进行区分的
Maven主项目和子项目管理的不同
  主项目jar包版本统一管理
<dependencyManagement>
  子项目不用指定jar包版本
指定当前项目的坐标:
<groupId>com.taobao.newLauncher</groupId>
<artifactId>newLauncher-biz-home</artifactId>
<name>newlauncher-biz-home</name>
<version>1.0.0</version>
<packaging>jar</packaging>
如果是主pom文件, packaging指定为pom
如果是要生成的war包,packaging指定为war

主项目指定子项目
    <modules>
       <module>newlauncher-biz-core</module>
       <module>newlauncher-biz-home</module>
  </modules>

子项目需要指定parent
  <parent>
       <artifactId>newLauncher</artifactId>
       <groupId>com.taobao.newLauncher</groupId>
       <version>1.0.0</version>
</parent>
Maven命令
mvn clean     删除target目录内容
mvn package   打包
mvn install   让项目真正引用生成的jar包
mvn test      执行测试用例
Maven插件
1)maven-compiler-plugin 指定JDK版本
      <plugin>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.3.2</version>
             <configuration>
              <source>1.6</source>
              <target>1.6</target>
              <showWarnings>true</showWarnings>
              <encoding>gbk</encoding>
             </configuration>
             <dependencies>
              <dependency>
                <groupId>org.codehaus.plexus</groupId>
                <artifactId>plexus-compiler-javac</artifactId>
                <version>1.8.1</version>
              </dependency>
             </dependencies>
       </plugin>

2)maven-war-plugin 打war包 在web子项目中指定

<build>         
    <finalName>launcher</finalName>
    <directory>target/jboss</directory>
     <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <webResources>
                        <resource>
<directory>src/main/resources</directory>
                            <filtering>true</filtering>
                            <includes>
                                <include>**/*.xml</include>
                                <include>**/*.vm</include>
                            </includes>
                            <targetPath>WEB-INF</targetPath>
                        </resource>  
                    </webResources>
                </configuration>
            </plugin>
        </plugins>
</build>

3)单元测试插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.6</version>
    <configuration>
       <testFailureIgnore>true</testFailureIgnore>
<!-单元测试失败依然打包-->
       <skip>true</skip> 是否过滤单元测试
       <!--<includes>
           <include>**/*Test.java</include>
       </includes>-->
       <excludes>
           <exclude>**/*Test.java</exclude>
         </excludes>
    </configuration>
</plugin>

4)jar包插件

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.3.1</version>
    <configuration>
       <archive>
          <addMavenDescriptor>false</addMavenDescriptor>
是否增加maven描述信息
           <manifest>
<addDefaultImplementationEntries>
true
</addDefaultImplementationEntries>
           </manifest>
           <manifestEntries>
          <Implementation-Build>${buildNumber}</Implementation-Build>
           </manifestEntries>
       </archive>
    </configuration>
</plugin>

5)resource插件  类似antx的auto-config功能
Mvn package –Pdev  dev即定义的ID
Antx中的属性文件在Maven中如何引用?
在主pom.xml中增加配置属性
<profiles>中可以有多个profile, id需唯一

<profiles>
       <profile>
           <id>dev</id>
           <properties>
           <props>launcher.properties.dev</props>
  <dependency.version.ext>-SNAPSHOT</dependency.version.ext>
           </properties>
           <activation>
              <activeByDefault>true</activeByDefault>
是否默认引用此文件 如果此项为true,引用此文件 参数-P可以不加
           </activation>
       </profile>
    </profiles>

根据外部属性文件填写需要过滤文件中的变量,默认会生成到classes目录

<filters>
           <filter>jdbc.properties.dev</filter>
Filter 配置项指定过滤那个文件?
1) 可以写文件名
2) 也可以写-P指定的profile ,
引用的是上述定义的profile dbc.properties.dev替换为../${props}
       </filters>
       <resources>
           <resource>
              <directory>src/main/resources</directory>
              <filtering>true</filtering>
true需要过滤,false不需要过滤
              <includes>  需要过滤的文件
                  <include>**/*.*</include>
                  <include>**/*</include>
              </includes>
              <excludes> 那些文件不需要移动到classes目录
              <exclude>**/*.xml</exclude>
              <exclude>**/*.vm</exclude>
              </excludes>
           </resource>
           <resource>
              <directory>src/main/java</directory>
              <includes>
                  <include>**/*.xml</include>
              </includes>
           </resource>
       </resources>
6)maven-eclipse-plugin插件
    <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-eclipse-plugin</artifactId>
                  <version>2.8</version>
                  <configuration>
                     <sourceExcludes>
                         <sourceExclude>**/.svn/</sourceExclude>
                     </sourceExcludes>
                     <downloadSources>true</downloadSources>
                  </configuration>
              </plugin>
Maven库配置
Maven从中央仓库下载这些资源文件,如果需要修改项目的下载地址,可以在主pom.xml文件中加入:
<repositories></repositories> 中任意增加repository仓库
<repository>
     <id>simba-nexus</id>
<url>http://mvnrepo.maven.com/nexus/content/repositories/releases</url>
     <releases>
       <enabled>true</enabled>
    </releases>
    <snapshots>
       <enabled>false</enabled>
    </snapshots>
</repository>



//////////////////////////////////////
http://hi.baidu.com/guorabbit/blog/item/40f3f2dca741d4aacc11667f.html

maven 根据不同的环境打war包-->资源文件的处理方式

发现犯的错误:

1. 指定了testResource 文件夹与resource 为同一个文件夹。导致不论在resource 里面如何过滤文件,都不起作用。资源文件本来就是共享的。不必这样指定。

2. 添加在<resources>
    <resource>
     <directory>src/resources</directory>
    </resource>
   </resources> 中的exclude 起作用。使用的路径为相对路径 。应该都是相对于pom 文件的路径 。但是这里使用的路径**/jdbc.properties 这种方法生效。

3. 添加在maven-war-plugin 中的<overlays>
       <overlay>
        <groupId>bingo.project.fap</groupId>
        <artifactId>bingo.aip.portal</artifactId>
        <excludes>
         <exclude>
          **/config/jdbc.properties
         </exclude>
        </excludes>
       </overlay>
      </overlays> 并没有生效。

4. 添加在maven-war-plugin 中的warSourceExcludes :

      <warSourceExcludes>
       **/jdbc.properties,**/acegi.properties
      </warSourceExcludes>

有效。可以使用, 分隔不同的文件。但是路径使用WebRoot起始和WEB-INF 都没有起作用。

这种打包的方式还有待试验。

猜你喜欢

转载自haiwei2009.iteye.com/blog/1114193