Maven 技术集合-持续更新

远程仓库直接配置为阿里云-提升访问速度
  • 方法1、修改 settings.xml 添加-提供两种方法-一个指定了阿里仓库具体仓库
<mirror>  
    <id>nexus-aliyun</id>  
    <mirrorOf>central</mirrorOf>    
    <name>Nexus aliyun</name>  
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>  
</mirror> 
或者

<mirror>  
      <id>nexus-aliyun</id>  
      <mirrorOf>*</mirrorOf>  
      <name>Nexus aliyun</name>  
      <url>http://maven.aliyun.com/nexus/content/groups/public</url>  
 </mirror>   
  • 方法2、修改settings.xml 添加
<repository>
    <id>nexus-aliyun</id>
    <name>Nexus aliyun</name>
    <layout>default</layout>
    <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
    <releases>
        <enabled>true</enabled>
    </releases>
</repository>
maven自定义war包名和打包路径-不设置就是项目名,默认路径位于target路径下

方法一和方法二可以同时配置,如果同时配置会生成两个war包,而且使用方法二生成的war包要小一些,建议只配置方法二
* 方法一

<build>
        <finalName>自定义名字1</finalName>
</build>
  • 方法二
<build>
        <plugins>   
            <plugin>  
               <groupId>org.apache.maven.plugins</groupId>  
               <artifactId>maven-war-plugin</artifactId>  
               <version>2.1.1</version>  
               <configuration>  
                    <!-- war包生成路径不是修改的,这里注释掉 -->
                   <!-- <webResources>  
                       <resource>  
                           this is relative to the pom.xml directory  
                           <directory>src/main/webapp</directory>  
                       </resource>  
                   </webResources>   -->
               <warName>自定义名字</warName>  
               </configuration>  
           </plugin>
        </plugins>
    </build>
maven 中配置 tomcat 插件

mvn tomcat:run

<pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat6-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  • settings.xml
<pluginGroups>
    <pluginGroup>org.apache.tomcat.maven</pluginGroup>
  </pluginGroups>
maven 中配置 jetty 插件

mvn jetty:run
* pom.xml

<build>    
        <!-- jar包名称 -->    
        <finalName>springmvc</finalName>    

        <plugins>    
            <!-- 编译jdk版本设置 1.6 -->    
            <plugin>    
                <groupId>org.apache.maven.plugins</groupId>    
                <artifactId>maven-compiler-plugin</artifactId>    
                <version>2.3.2</version>    
                <configuration>    
                    <source>1.6</source>    
                    <target>1.6</target>    
                    <uriEncoding>${project.build.sourceEncoding}</uriEncoding>    
                </configuration>    
            </plugin>    

            <!-- jetty插件 -->    
            <!-- 在settings.xml 文件中新增 <pluginGroup>org.mortbay.jetty</pluginGroup>      
            <pluginGroups>  
            <pluginGroup  
                 | Specifies a further group identifier to use for plugin lookup.  
                <pluginGroup>com.your.plugins</pluginGroup>  
                <pluginGroup>org.mortbay.jetty</pluginGroup>      
            </pluginGroups>  
          -->  
            <plugin>    
                <groupId>org.mortbay.jetty</groupId>    
                <artifactId>maven-jetty-plugin</artifactId>    
                <version>6.1.26</version>    
                <configuration>    
                    <!-- 通过jetty访问项目时的项目名称 ${project.build.outputDirectory} 为war包名 -->    
                    <webApp>    
                        <contextPath>${project.build.outputDirectory}</contextPath>    
                    </webApp>    
                    <!-- jetty附属配置,未启用 -->    
                    <!-- <webDefaultXml>src/main/resources/webdefault.xml</webDefaultXml> -->    
                    <!-- configuration.scanIntervalSeconds 配置表示新代码的扫描时间间隔(秒),值 <= 0 表示不扫描 -->    
                    <scanIntervalSeconds>0</scanIntervalSeconds>    
                    <!-- 端口设置 -->    
                    <connectors>    
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">    
                            <port>8085</port>    
                        </connector>    
                    </connectors>    
                </configuration>    
            </plugin>    

        </plugins>    
    </build>   
  • settings.xml
<pluginGroups>  
<pluginGroup>org.mortbay.jetty</pluginGroup>  
</pluginGroups>  
eclipse 配置 Maven 插件

http://jingyan.baidu.com/article/5d368d1e306a913f61c05743.html

maven 聚合和继承的关系-既可以相互独立又可以结合使用

maven 聚合是对于父模块而言的,包含子模块,集中编译打包等;maven 继承是对于子模块而言的,子模块继承父模块的 pom,集中依赖版本。二者不是强联系,即父模块不知谁继承了它,子模块不知谁聚合了它。

dependencies与dependencyManagement的区别

父级pom.xml,< packaging>pom< /packaging>
使用< dependencyManagement>,设定依赖以及依赖版本号,其在< dependencies>的外围。
子项目< parent>继承父项目的pom.xml,需要写出自己的依赖,但是不用写明依赖版本号了。
只使用dependencies的话,即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
参考:http://blog.csdn.net/liutengteng130/article/details/46991829

依赖传递-可选依赖 optional

项目B 依赖 项目A
项目C依赖项目B
常规来说,项目C依赖于项目A
但是如果项目C不需要项目A,则可以在项目B中对A的依赖配置中增加

< optional >true < / optional ><!-- 传递依赖-true 
依赖本项目的项目需要重新配置本项目的依赖 -->

完整的依赖配置举例

 <dependency>
        <groupId>groupname</groupId>
        <artifactId>artifactIdname</artifactId>
        <version>version</version>
        <optional>true</optional><!-- true,可选依赖,一下本项目的项目如有需要需从新引入本依赖 -->
     </dependency>
Maven build\clean\generate-sources\install

Maven build是这个插件让你自己去配置执行目标的。
Maven clean 清除上一次Maven执行的结果
Maven generate-sources会根据pom配置去生成源代码格式的包
Maven install将项目输出构件部署到本地仓

多版本控制深入:profile 和 资源过滤 实现多环境数据库配置

正常来说,我们一般需要jdbc.properties 文件

#jdbc settings
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false
jdbc.username=root
jdbc.password=root

Spring 中配置加载 常量文件

 <!-- 定义受环境影响易变的变量 -->
    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="ignoreResourceNotFound" value="true" />
        <property name="locations">
            <list>
                <!-- 数据库常量配置 -->
                <value>classpath:config/jdbc.properties</value>
            </list>
        </property>
    </bean>

如果我们采取Maven 的多版本配置,比如测试环境、生产环境和开发环境使用的数据库是不一样的
变化在于: jdbc.properties 的取值从 Maven 的 pom.xml 中获取(${maven.jdbc.driverClassName}从那里来?别着急)

  • Maven的pom.xml 内容
<!-- 多版本控制 -->
   <profiles>

    <!-- 开发环境 profile  -->
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <!-- 数据库配置 -->
            <maven.jdbc.driverClassName>com.mysql.jdbc.Driver</maven.jdbc.driverClassName>
            <maven.jdbc.url>jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false</maven.jdbc.url>
            <maven.jdbc.username>root1</maven.jdbc.username>
            <maven.jdbc.password>root2</maven.jdbc.password>
        </properties>
    </profile>

    <!-- 测试环境 profile  -->
    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <!-- 数据库配置 -->
            <maven.jdbc.driverClassName>com.mysql.jdbc.Driver</maven.jdbc.driverClassName>
            <maven.jdbc.url>jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false</maven.jdbc.url>
            <maven.jdbc.username>root</maven.jdbc.username>
            <maven.jdbc.password>root</maven.jdbc.password>
        </properties>
    </profile>
   </profiles>

激活方式有多种,这里选择默认激活方式,仅一个配置可被激活

<!-- profile 多版本定义 -->
   <profiles>
    <profile>
        <id>test</id>
        <activation>
            <activeByDefault>false</activeByDefault><!-- 默认激活 false 否 -->
        </activation>
        <build>  
            <finalName>test</finalName><!-- 打包名字 -->
        </build>  
    </profile>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault><!-- 默认激活 true 是-->
        </activation>
        <build>
            <finalName>dev</finalName><!-- 打包名字 -->
        </build>
    </profile>
   </profiles>
Maven 生成站点信息
<!-- 生成项目站点 运行 mvn:site-->
    <dependency>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-site-plugin</artifactId>
        <version>3.0-beta-1</version>
    </dependency>

运行 mvn:site,即可在 target 目录下看到相关文件

  • 如果你想看到生成中文文档
<build>
    <plugins>   
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-site-plugin</artifactId>
            <configuration>
                <locales>zh_CN</locales>
            </configuration>    
        </plugin>
    </plugins>  
  </build>  

这样就结束了吗?因为我们在Maven中配置的常量并不能直接用于Java项目中解析,所以需要开启资源过滤设置

虽然默认的Maven资源是 src/main/resources和src/test/resources 但是依旧需要设置一下,当然,多设置也是可以的,比如你有src/dev/resources

<build>  
    <!-- 开启资源过滤-正式 -->
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <!-- 测试 -->
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
  </build>

最后需要注意的是
第一:配置版本的常量名字不能和jdbc.properties 的名字一样,即jdbc.properties的key和 pom.xml的properties的值不能一样
第二:更换profile的内容需要clean项目,否则新配置不会自动生效

Maven 指定测试目录
<!-- junit仅测试环境使用     -->
     <dependency>    
         <groupId>junit</groupId>    
         <artifactId>junit</artifactId>    
         <version>4.12</version>    
         <scope>test</scope>    
     </dependency>  
 <!-- 测试插件 -->
         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.5</version>
            <configuration>
                <skipTests>false</skipTests><!-- 跳过测试 true-是,false-否 -->
                <test>com.bestcxx.stu.springmybatis.service.*,com.bestcxx.stu.springmybatis.dao.*</test><!-- 指定包路径进行测试 -->
            </configuration>
         </plugin>
  • 运行命令

mvn:test

学Maven 不可不知的网站-获取依赖

http://mvnrepository.com/search?q=slf4j-log4j12

aspectj Maven 依赖
 <dependency>  
          <groupId>org.aspectj</groupId>  
          <artifactId>aspectjrt</artifactId>  
          <version>1.8.9</version>  
        </dependency>  
        <dependency>  
          <groupId>org.aspectj</groupId>  
          <artifactId>aspectjtools</artifactId>  
          <version>1.8.9</version>  
        </dependency>  
        <dependency>  
          <groupId>org.aspectj</groupId>  
          <artifactId>aspectjweaver</artifactId>  
          <version>1.7.4</version>  
        </dependency>  
scope=”system”,将指定目录jar包通过maven 形成依赖
<dependency>
            <systemPath>本地路径.jar</systemPath>
            <groupId>***</groupId>
            <artifactId>***</artifactId>
            <version>***</version>
            <scope>system</scope>
        </dependency>

猜你喜欢

转载自blog.csdn.net/bestcxx/article/details/81712474