您的位置:首页 > 编程语言 > Java开发

Maven 使用心得

2015-10-21 00:00 537 查看
摘要: Maven的一些使用心得,以及Maven结合Eclipse使用的一些心得。

本篇不介绍什么是Maven,以及怎么使用Maven,只简单分享记录下我的Maven经验。

Eclipse Maven 插件
最新版的 Eclipse 已经集成了 Maven,如果你需要 Eclipse Maven 插件,戳下面:
http://pan.baidu.com/s/1c0FLDnE (覆盖 Eclipse 中的相应目录即可)

Eclipse Properties Editor 插件
Eclipse 中 .properties 属性文件,默认打开是被编码过的,如果有汉字,我们无法明确的看出写的是什么,通过这个插件问题就解决了。
Eclipse 通过安装插件地址:http://propedit.sourceforge.jp/eclipse/updates/
Eclipse 插件包:http://pan.baidu.com/s/1A0cpS (覆盖 Eclipse 中的相应目录即可)

Eclipse OpenExplorer 插件
可以直接 Eclipse 中文件所在文件夹
http://pan.baidu.com/s/1eQfPIBS (把文件放到 Eclipse 下 dropins 目录)

Eclipse 中 Maven 项目,Alt + F5 之后编译级别问题
Alt + F5 之后项目的编译级别就变成了 jdk 1.5,在 setting.xml 的 profiles 标签里加入如下代码,这是全局设置

<profile>
<id>jdk-1.6</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.6</jdk>
</activation>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
</properties>
</profile>
也可以在 项目的 POM 文件中添加如下配置(项目级别设置,未测):

<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.compilerVersion>1.6</maven.compiler.compilerVersion>
</properties>


Maven 排除一个传递依赖
例如我们使用 Dubbo 时,由于 Dubbo 依赖的 Spring 版本较低,可能和我们本地的 Spring 冲突,我们通过配置让因引入 Dubbo jar 包时不引入它依赖的 Spring,POM文件中:

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${alibaba.dubbo.version}</version>
<exclusions>
<exclusion>
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>


Maven Nexus 私服安装配置
http://my.oschina.net/ironwill/blog/521225

安装JAR包到本地Maven仓库
mvn install:install-file -Dfile=xxx.jar -DgroupId=yyy -DartifactId=zzz -Dversion=0.1 -Dpackaging=jar -DgeneratePom=true -DcreateChecksum=true

Maven 一些常用插件

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- 跳过单元测试 -->
<skip>true</skip>
<!-- 忽略单元测试失败 -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin


<!-- 指定maven编译编译级别 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>


<!-- Maven 打包插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!-- dependencies-lib这个目录名可自定义,这个目录下放的是项目jar文件依赖的jar包,这个目录和项目jar文件同级 -->
<classpathPrefix>dependencies-lib/</classpathPrefix>
<mainClass>com.lee.util.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>


<!-- 把maven依赖的jar copy到指定目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<!-- 可自定义 -->
<id>copy dependencies jar</id>
<!-- maven 执行 package 命令时出发此插件运行,还可以是install等命令 -->
<phase>package</phase>
<goals>
<!-- 默认配置此插件执行时运行哪个目标 -->
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- 执行此插件的目标时传递的参数 -->
<outputDirectory>
<!-- Maven内置变量 -->
${project.build.directory}/dependencies-lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>


<!-- 用来创建你应用程序特有分发包的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<!-- 指定程序入口 -->
<mainClass>com.lee.util.App</mainClass>
</manifest>
</archive>
</configuration>
<!-- 配置maven执行某些命令时,触发此插件 -->
<executions>
<execution>
<!-- 自定义唯一标识 -->
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin


如果 Maven 伺服配置完成,发布一个jar包到伺服的配置如下

要发布的项目 POM 文件中加入如下

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


<!-- Maven Setting 文件中加入 -->
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>


<mirror>
<id>myNexus</id>
<mirrorOf>*</mirrorOf>
<name>My Nexus Mirror</name>
<url>http://192.168.0.147:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</mirror>


<!-- 通过上面的mirror配置,项目已经能从伺服下载jar包和插件,但不能下载snapshots版本的jar,通过以下配置开启 -->
<profile>
<!-- 从指定伺服下载jar包,此处开启了snapshots版本的jar包下载 -->
<id>use-local-nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://192.168.0.147:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!-- 指定插件也从伺服下载 -->
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://192.168.0.147:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息