您的位置:首页 > 其它

Maven 技术集合-持续更新

2015-11-12 16:10 330 查看

·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>

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

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

·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 生成站点信息

<!-- 生成项目站点 运行 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>


·多版本控制深入: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}从那里来?别着急)

jdbc.driverClassName=${maven.jdbc.driverClassName}
jdbc.url=${maven.jdbc.url}
jdbc.username=${maven.jdbc.username}
jdbc.password=${maven.jdbc.password}


${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>


这样就结束了吗?因为我们在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项目,否则新配置不会自动生效

·profile Maven 多版本定义-打包名字举例

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

<!-- 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 build\clean\generate-sources\install

Maven build是这个插件让你自己去配置执行目标的。

Maven clean 清除上一次Maven执行的结果

Maven generate-sources会根据pom配置去生成源代码格式的包

Maven install将项目输出构件部署到本地仓

·依赖传递-可选依赖 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>


·dependencies与dependencyManagement的区别

父级pom.xml,<packaging>pom</packaging>

使用<dependencyManagement>,设定依赖以及依赖版本号,其在<dependencies></dependencies>的外围。

子项目<parent></parent>继承父项目的pom.xml,需要写出自己的依赖,但是不用写明依赖版本号了。

只使用dependencies的话,即使在子项目中不写该依赖项,那么子项目仍然会从父项目中继承该依赖项(全部继承)
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom;另外如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。

参考:http://blog.csdn.net/liutengteng130/article/details/46991829

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

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

·eclipse 配置 Maven 插件

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



·maven中配置jetty 插件

1、在 pom 文件中增加

[html] view
plain copy

<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>    

2、在 maven 的 setting.xml 文件中增加

[html] view
plain copy

<!-- pluginGroups  

 | This is a list of additional group identifiers that will be searched when resolving plugins by their prefix, i.e.  

 | when invoking a command line like "mvn prefix:goal". Maven will automatically add the group identifiers  

 | "org.apache.maven.plugins" and "org.codehaus.mojo" if these are not already contained in the list.  

 |-->  

<pluginGroups>  

  <!-- pluginGroup  

   | Specifies a further group identifier to use for plugin lookup.  

  <pluginGroup>com.your.plugins</pluginGroup>  

  -->  

<pluginGroup>org.mortbay.jetty</pluginGroup>  

</pluginGroups>  

·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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: