Maven

Maven: The Complete Reference:
[b][url]http://www.sonatype.com/books/mvnref-book/reference/public-book.html[/url][/b]

Maven Repository Management & Nexus on local:
[url]http://wuaner.iteye.com/blog/1136267[/url]

Maven Documentation:
[b][url]http://maven.apache.org/guides/index.html[/url][/b]

Introduction to the Standard Directory Layout:
[url]http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html[/url]


几个默认值:
project的默认打包类型:jar
dependency 的默认 scope:compile


Specifying resource directories:
[url]http://maven.apache.org/plugins/maven-resources-plugin/examples/resource-directory.html[/url]
Maven Resources plugin > Including and excluding files and directories:
[url]http://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html[/url]


[b]常用命令:[/b]

#下载项目依赖的所有 jars 的 sources 和 javadoc (http://stackoverflow.com/questions/2059431/get-source-jars-from-maven-repository)
$ mvn dependency:sources
$ mvn dependency:resolve -Dclassifier=javadoc
#将class文件jar包install到Local Repo.的同时也将该jar包对应的sources包也install到Local Repo;生成的源代码包的包名为 [二进制class文件包名]-sources.jar
mvn source:jar install
# 查看一个 plugin(这里是maven-compiler-plugin) 的所有 goals 及 goals 的 parameters:
$ mvn help:describe -Dplugin=compiler -Ddetail
# 查看一个 goal(这里是compiler:compile) 的说明及 parameters:
$ mvn help:describe -Dcmd=compiler:compile -Ddetail
# 生成一个 maven 项目
$ mvn archetype:create -DgroupId=com.lxg.demo -DartifactId=sample-project //archetype:create 这个 goal 已经 deprecated 了,请使用 archetype:generate (http://stackoverflow.com/questions/6328778/how-to-create-an-empty-multi-module-maven-project)
$ mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes -DarchetypeArtifactId=pom-root -DarchetypeVersion=RELEASE
$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=RELEASE -DarchetypeCatalog=internal
$ mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeVersion=RELEASE -DarchetypeCatalog=internal
# lists the profiles which are currently active for the build.
$ mvn help:active-profiles
# lists the available profiles under the current project.
$ mvn help:all-profiles
# using designated profile(dev) to execute phase
$ mvn -P dev package
# skip test & javadoc,使用socks代理
$ mvn clean package -DskipTests -Dmaven.javadoc.skip=true -DsocksProxyHost=127.0.0.1 -DsocksProxyPort=1080
# install a external jar file to local repo.(http://whiteycode.blogspot.hu/2012/05/building-hadoop-source-jars.html http://maven.apache.org/plugins/maven-install-plugin/usage.html)
$ mvn install:install-file -D...
$ mvn install:install-file -Dfile=sqljdbc41.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.1
# 检查项目代码
$ mvn checkstyle:checkstyle
# View the default phase that a goal of a maven plugin binds to (<build><plugins><plugin><executions><execution> 下若写了goal却未指定phase,则该goal既是绑定到其默认phase上. http://stackoverflow.com/questions/12309652/how-can-i-find-out-the-default-phase-a-maven-goal-binds-to)
mvn help:describe -DartifactId=maven-compiler-plugin -DgroupId=org.apache.maven.plugins -Dgoal=compile -Ddetail



[color=red][b]将maven项目import到eclipse中,或者使用eclipse插件来做(如m2eclipse),或者使用maven的maven-eclipse-plugin来做,不要试着混合使用它们:[/b][/color]
[url]https://bugs.eclipse.org/bugs/show_bug.cgi?id=374332#c14[/url][quote]The best I can offer is to block import of projects that use maven-eclipse-plugin (in fact, this is how m2e 0.12 behaved). m2e and maven-eclipse-plugin implement two distinct and incompatible maven/eclipse integration approaches. Don't try to mix them for the same project.[/quote]The eclipse plugin for Maven: m2eclipse:
[url]http://m2eclipse.sonatype.org/[/url]
Maven Eclipse Plugin:
[url]http://maven.apache.org/plugins/maven-eclipse-plugin/[/url][quote]为eclipse的java - build path - classpath variables 添加一个名为M2_REPO的classpath,指向本地的maven reop(注意该goal的workspace参数是必填的):

#该goal代替了maven 2.x中的eclipse:add-maven-repo
mvn -Declipse.workspace="/home/username/.m2/repository" eclipse:configure-workspace

生成.project & .classpath & .setting/org.eclipse.jdt.core.prefs 等eclipse项目的配置文件和目录(参数buildOutputDirectory和useProjectReferences必填但有默认值):
mvn eclipse:eclipse

清理掉maven项目对eclipse的支持:
mvn eclipse:clean 
[/quote]maven插件 maven-eclipse-plugin 和eclipse插件 m2eclipse & IAM 比较:
[url]http://docs.codehaus.org/display/MAVENUSER/Eclipse+Integration[/url]
从开发人员活跃度和功能支持上来看,m2eclipse是不错的maven和eclipse集成的工具。
m2eclipse的问题:
在使用m2eclipse时,当导入的maven项目为mutiple modules时,有一个非常不好用的地方就是:eclipse里的项目文件搜索,老是出现重复的文件,因为eclipse认为它们在不同的项目中,但其实是同一个文件。具体解决办法见本:
[url]http://wuaner.iteye.com/blog/444391[/url]
eclipse中maven 项目的classpath问题:
当执行src/main/java下的一个源码文件的main方法时,只有src/main/java和src/main/resources下的源代码/资源文件(即[b]outputDirectory:${project.build.directory}/classes[/b])才是main方法运行的classpath;而执行src/test/java下的单元测试代码,[b]outputDirectory:${project.build.directory}/classes和testOutputDirectory:${project.build.directory}/test-classes[/b](src/main/java和src/main/resources下的会build到前一个下,src/test/java和src/test/resources会build到后一个下)都会作为classpath。


[color=red][b]POM:[/b][/color]
[b][url]http://maven.apache.org/pom.html[/url][/b]
[img]http://dl.iteye.com/upload/attachment/0074/5231/e7555bef-a702-3460-8ebe-380d3cafc155.png[/img]
Maven的所有 pom.xml 文件隐式继承 [i]MAVEN_INSTALL_DIR/lib/maven-model-builder-3.0.3.jar:org/apache/maven/model/pom-4.0.0.xml[/i],该文件被称为super POM):[quote][url]http://maven.apache.org/ref/3.0.4/maven-model-builder/super-pom.html[/url]

<project>
<modelVersion>4.0.0</modelVersion>

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>central</id>
<name>Central Repository</name>
<url>http://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
<releases>
<updatePolicy>never</updatePolicy>
</releases>
</pluginRepository>
</pluginRepositories>

<build>
<directory>${project.basedir}/target</directory>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
<finalName>${project.artifactId}-${project.version}</finalName>
<testOutputDirectory>${project.build.directory}/test-classes</testOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<scriptSourceDirectory>src/main/scripts</scriptSourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/java</testSourceDirectory>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>${project.basedir}/src/test/resources</directory>
</testResource>
</testResources>
<pluginManagement>
<!-- NOTE: These plugins will be removed from future versions of the super POM -->
<!-- They are kept for the moment as they are very unlikely to conflict with lifecycle mappings (MNG-4453buildOutputDirectory:buildOutputDirectory:buildOutputDirectory:buildOutputDirectory:buildOutputDirectory:buildOutputDirectory:) -->
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>

<reporting>
<outputDirectory>${project.build.directory}/site</outputDirectory>
</reporting>

<profiles>
<!-- NOTE: The release profile will be removed from future versions of the super POM -->
<profile>
<id>release-profile</id>
...
</profile>
</profiles>

</project>
[b]Complete POM structure:
[url]http://maven.apache.org/ref/3.0.3/maven-model/maven.html[/url][/b]
从中可以看出:
maven默认编译/输出以下四个路径的源代码/资源文件:
src/main/java
src/main/resources
src/test/java
src/test/resources
$ mvn compile只会编译src/main/java & src/main/resources,输出到
$ mvn test-compile会编译以上四个。
欲编译其他的源代码路径,请在pom.xml的<build>下添加:

<build>
<resources>
<resource>
<directory>src/config/resources</directory>
</resource>
</resources>
</build>
[/quote]


[color=red][b]Repository:[/b][/color]
Maven Central Repository Browser:
[url]http://search.maven.org/#browse[/url]
[url]http://juvenshun.iteye.com/blog/359256[/url]


[color=red][b]Dependency:[/b][/color]
[url]http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope[/url][quote]The minimal set of information for matching a dependency reference against a dependencyManagement section is actually [b]{groupId, artifactId, type, classifier}[/b]. In many cases, these dependencies will refer to jar artifacts with no classifier. This allows us to shorthand the identity set to [b]{groupId, artifactId}[/b], since the [b]default for the type field is jar[/b], and the [b]default classifier is null[/b].[/quote]<dependencyManagement> 和 top-level <dependencies> 的区别:
[url]http://stackoverflow.com/questions/2619598/differences-between-dependencymanagement-and-dependencies-of-maven[/url][quote]<dependencyManagement> 并不实际引入依赖,它只是提供了统一集中的管理依赖项s的version、scope、type、exclusions等信息的功能;好处在于,当你在子项目中实际引入一个出现在父项目 <dependencyManagement> 中的 project 作为依赖时,你只需要填写其groupId和artifactId即可。[/quote]关于BOM(Bill Of Materials)的介绍:
[url]http://howtodoinjava.com/maven/maven-bom-bill-of-materials-dependency/[/url]
A BOM dependency keep track of version numbers and ensure that all dependencies (both direct and transitive) are at the same version.


[color=red][b]Plugins & Goals & Mojo(Maven plain Old Java Object)[/b][/color]
[url]http://www.sonatype.com/books/mvnex-book/reference/simple-project-sect-simple-core.html[/url]
[url]http://maven.apache.org/guides/plugin/guide-java-plugin-development.html[/url]
[url]http://maven.apache.org/guides/introduction/introduction-to-plugin-prefix-mapping.html[/url]
[url]http://maven.apache.org/guides/mini/guide-configuring-plugins.html#Using_the_executions_Tag[/url]
maven的plugin就是一个packaging类型为 "maven-plugin" 的普通maven project;一个plugin可以有多个goal;用来定义goal的的文件我们称为Mojo,Mojo文件通常使用annotated Java classes or Beanshell script。
运行一个plugin的goal的标准格式为:[b]mvn groupId:artifactId:version:goal[/b]。如对下面的这个maven plugin:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>sample.plugin</groupId>
<artifactId>hello-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>
<name>Sample Parameter-less Maven Plugin</name>
<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</project>
其有一个用Mojo文件定义的名为sayhi的goal,则运行这个goal的标准格式为:
mvn sample.plugin:hello-maven-plugin:1.0-SNAPSHOT:sayhi

省略版本version的情况下,则默认运行该plugin的最新版:
mvn sample.plugin:hello-maven-plugin:sayhi

更进一步,如果你自定义的plugin的artifactId的命名遵循形如 maven-${prefix}-plugin 或 ${prefix}-maven-plugin (约定优于配置!)的格式, 则直接可以使用该plugin的artifactId中的prefix来代表这个plugin来运行其的某特定goal:
mvn hello:sayhi
同时,你可也可以通过在为该plugin指定<goalPrefix>来指定不同的prefix;
通过将该pugin的groupId加入maven setting.xml文件(per-user: ~/.m2/settings.xml; global: $M2_HOME/conf/settings.xml) 中的 <pluginGroups>,则该group会作为mvn命令行中省略groupId运行一个plugin的goal(即形如${prefix}:goal)时的默认搜索group;注意setting.xml的<pluginGroups>默认是隐式包含 org.apache.maven.plugins 和 org.codehaus.mojo 这两个groupId的:
[b]org.apache.maven.plugins 这个group下的内置plugins如下:
[url]http://maven.apache.org/plugins/index.html[/url]
org.codehaus.mojo 这个group下的内置plugins如下:
[url]http://mojo.codehaus.org/plugins.html[/url][/b]
[b]因为默认包含这两个 groupIds,所以这两个 groupIds 下的所有 plugins 都是可以在 mvn 命令行下直接使用的!你在 pom.xml-<build><plugins> 下对属于这两个 groupIds 的某个 plugin 做配置,[color=red]只是 override 其默认配置、将其绑定到某个 Build lifecycle 的 phase 上等[/color]而已,并不是说只有在 pom.xml-<build><plugins> 下配置了某个 plugin,该 plugin 才可以被使用![/b]
总结后,运行一个plugin的goal的格式可以是:

mvn groupId:artifactId:version:goal
mvn groupId:artifactId:goal
mvn ${prefix}:goal (前提是artifactId遵循上面说的约定优于配置的原则)
#经验证,下面的运行方式都不行:

mvn groupId:${prefix}:goal
mvn artifactId:goal
[b]<pluginManagement> 和 top-level <plugins> 的区别:[/b]
[url]http://stackoverflow.com/questions/10483180/maven-what-is-pluginmanagement[/url][quote]同 <dependencyManagement> 和 top-level <dependencies> 的区别一样,<pluginManagement> 只是提供了集中管理项目的 plugins 配置项的功能,在对 plugin 的实际调用中并不起作用;top-level <plugins> 里的 plugins 才是实际调用的 plugins。 [/quote]


[color=red][b]Build Lifecycle & Phases[/b][/color]
[url]http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference[/url]
[b][url]http://juvenshun.iteye.com/blog/213959[/url][/b][quote]Maven有三套相互独立的生命周期,分别是Clean、Default和Site。你可以仅仅调用clean来清理工作目录,仅仅调用site来生成站点。当然你[b]也可以直接运行 mvn clean install site 运行所有这三套生命周期[/b]。
每套生命周期都由一组阶段(Phase)组成,我们平时[b]在命令行输入的命令总会对应于一个特定的阶段[/b]。比如,运行mvn clean ,这里的clean是Clean生命周期的一个阶段。
[color=red][b]需要特别强调的是,在一个生命周期中,运行某个阶段的时候,它之前的所有阶段都会被运行,也就是说,mvn clean 等同于 mvn pre-clean clean,如果我们运行 mvn post-clean ,那么 pre-clean,clean 都会被运行。这是Maven很重要的一个规则,可以大大简化命令行的输入。[/b][/color]
[color=red]我的理解:[/color]
[i]mvn command[/i],command可能是个goal或者phase。
goal比较容易理解:goal是plugin的东西,是一个plugin中的Mojo文件(moji文件就是goal的定义文件,多由java文件或bellshell脚本编写)实现的特定目标。可以说:Mojo即goal。
goal也是可以单独调用的,调用的标准格式为:mvn groupId:artifactId:version:goal(如org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile)。在遵从了“约定优于配置“的前提后,这种对goal的直接调用的格式最终可以简化为:${prefix}:goal(如compiler:compile),其中${prefix}是artifactId的约定格式“maven-${prefix}-plugin”的中间部分。
需要好好理解的是phase作为command的情况:maven中任何一个作为command运行的phase,一定只属于某一个特定的Build Lifecycle!我的理解是,Build Lifecycle这种东西,其实它[b]唯一的作用[/b]就是“定义了一个存在特定顺序的功能操作序列,对序列上任何一个功能操作的调用,都是自序列起点始、该功能操作点终的所有功能操作的顺序执行”,不存在什么“目前处于xx lifecycle”的概念。对phase的调用你可以任意的组合,如:mvn clean compile site、mvn comipe clean、mvn test compile clean、mvn test clean (最后两个需要注意,如果是个multiple modules的项目,则要注意将test和clean组合起来用并不是将所有作为module的项目都test完之后再做clean的,所以可能出现:B项目通过dependency使用了A项目classpath(通俗说即jar包)下的资源文件或者java可执行文件,B项目被test时,A项目已经被clean,则test B时会报找不到依赖的资源文件或者java可执行文件的错误。)
最后,[color=red][b]lifecycle的phase都和一个特定的goal相绑定,亦即,phase所提供的功能,其实是其所绑定的goal完成的。[/b][/color]如 default lifecycle的compile这个phase,绑定的就是compiler:compile(maven-compiler-plugin插件的compile这个goal)。加上引用楼主的话,要强调的是:[b]单独执行一个特定的goal,和单独执行一个与该goal绑定的某lifecycle的phase,过程及结果不见得是相同的,因为“在一个生命周期中(一个phase一定只属于三种Build Lifecycle中的一种,所以,运行这个phase,你可以说成是"在该phase对应的生命周期中"),运行某个phase的时候,它之前的所有phase都会被运行”[/b]。
[/quote]


[color=red][b]Profile:[/b][/color]
[b]children projects 默认继承 parent project 的 pom.xml 中定义的 profile。[/b]
exclusive profiles (若通过 -P 随意指定使用的profiles,则独占排它profile怎么实现那?待解决):
[url]http://maven.40175.n5.nabble.com/m2-exclusive-profiles-td78000.html[/url]
[url]http://stackoverflow.com/questions/12721732/creating-exclusive-profiles-in-maven[/url]
Profile 最佳实践:
[url]http://java.dzone.com/articles/maven-profile-best-practices[/url][quote]The build must pass when no profile has been activated
Never use <activeByDefault>
Use profiles to manage build-time variables, not run-time variables and not (with rare exceptions) alternative versions of your artifact[/quote][b]使用Profile做Resources filter:[/b]
[url]http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html[/url]
[url]http://www.inweb.de/chetan/_blog/2010/08/27/maven-filtering-with-deployment-profiles.html[/url]
assemply 中使用 filter:
[url]http://maven.apache.org/plugins/maven-assembly-plugin/examples/single/filtering-some-distribution-files.html[/url]
在某个特定的 profile 中 skipTests:
[url]http://stackoverflow.com/questions/772735/skipping-tests-in-some-modules-in-maven[/url][quote]

<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
<maven.test.skip>true</maven.test.skip>
</properties>
</profile>
or

<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
[/quote][b]注意:[/b]
一 3.0.4 以下版本的 maven(已知 3.0.3 就有这个问题),在使用 Profile + Resources Filter 做资源文件过滤时,如果被过滤的文件中含有 @ 这个字符,则该文件里应该被 filtering掉 的 ${xxx} 不会被 filtering,这是 3.0.4 以下版本 maven 的一个bug,见:
[url]http://blog.lick-me.org/2010/10/maven-3-resource-filtering-weirdness/[/url]
[url]http://maven.apache.org/plugins/maven-resources-plugin/examples/escape-filtering.html[/url]
二 当使用 eclipse m2e 插件,在 eclipse tomcat 下在本地部署项目 war 包时,可能会出现 Resources Filter 在部署到本地 tomcat 的 war 里不起作用的问题,这时需要为 eclipse 安装 m2e-wtp 插件:

Name:m2e-wtp releases repository
Location:http://download.eclipse.org/m2e-wtp/releases/
完整 Profile + Resources Filter:

<project>

<build>
<resources>
<!-- 这两个 resource 定义起的作用是:对 ..(src/main/resources及其子目录) 下所有以 .properties 结尾的文件,做资源过滤 (Resource Filtering,即将文件中 ${xxx} 形式的变量xxx替换为pom或系统变量等中对应的value); 而对除此之外的其他所有文件,不做 Resource Filtering -->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>**/*.properties</exclude>
</excludes>
</resource>
</resources>
</build>

<profiles>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
<activation>
<property>
<name>!production</name>
</property>
</activation>
<build>
<filters>
<filter>../src/main/filters/filter-${environment}.properties</filter>
</filters>
</build>
</profile>
<profile>
<id>prod</id>
<properties>
<environment>prod</environment>
</properties>
<activation>
<property>
<name>production</name>
</property>
</activation>
<build>
<filters>
<filter>../src/main/filters/filter-${environment}.properties</filter>
</filters>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>



[b][color=red]Other要点:[/color][/b]
[b]一[/b] maven projects间不允许存在循环依赖 cyclic reference ,否则会报:
[ERROR] The projects in the reactor contain a cyclic reference: Edge between 
[url]http://blog.csdn.net/kylinjade/article/details/5757219[/url]
[b]二[/b] 只要是打入jar包的,不管是java class文件还是资源文件(如applicationContext.xml、log4j.xml,甚至jdbc.properties、configuration.properties,这里甚至两字是为了强调相对前两个,后两个是否应该packaging入jar包中),都是可以被dependency的:
[url]http://stackoverflow.com/questions/12461302/spring-dependency-maven-multi-module[/url]
[b]三 [/b]mvn clean是清理项目的target输出目录,那怎么对local repository做clean那?
下面两个goal都可以用来做对local repo的clean:
dependency:purge-local-repository:
[url]http://maven.apache.org/plugins/maven-dependency-plugin/purge-local-repository-mojo.html[/url]
build-helper:remove-project-artifact:
[url]http://mojo.codehaus.org/build-helper-maven-plugin/remove-project-artifact-mojo.html[/url]
或者你也可以使用nexus:
[url]http://stackoverflow.com/questions/11830369/how-to-clean-maven-repository-at-amazon-s3[/url]
[b]四[/b] 忽略单元测试:
[url]http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html[/url]
[url]http://www.inze.be/andries/2011/05/11/maven-difference-between-dskiptests-and-dmaven-test-skiptrue/[/url]

mvn install -DskipTests
---Or
mvn install -Dmaven.test.skip=true
[b]五[/b] 实际开发中可以会将多个项目的配置文件统一抽取到一个独立的、assembly maven 项目,便于以后的管理;这里假设这样的项目的artifactId为 conmanyName-configuration,打包类型为 tar.gz;则当在具体的项目中需要用到该 统一配置项目 中的配置项时(aka,dependency on a tar.gz),该怎么办那?答案是可以使用 maven-dependency-plugin 的 unpack-dependencies goal:
[url]http://stackoverflow.com/questions/5388661/unzip-dependency-in-maven[/url]
[url]http://maven.apache.org/plugins/maven-dependency-plugin/examples/unpacking-project-dependencies.html[/url]

<project>

...

<dependencies>
<dependency>
<groupId>com.companyName</groupId>
<artifactId>companyName-configuration</artifactId>
<version>1.0.0</version>
<classifier>bin</classifier>
<type>tar.gz</type>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>unpack-global-configuration</id>
<phase>package<!-- or any other valid maven phase --></phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeGroupIds>com.companyName</includeGroupIds>
<includeArtifactIds>companyName-configuration</includeArtifactIds>
<outputDirectory>
${project.build.directory}/wherever/you/want/it
<!-- or: ${project.basedir}/wherever/you/want/it -->
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
另外一个使用 unpack-dependencies goal 对 dependency 的 sources.jar 做处理的例子:
[url]http://maven.apache.org/plugins/maven-dependency-plugin/examples/using-dependencies-sources.html[/url]


[b][color=red]Q & A:[/color][/b] 待整理完善
[color=red]mvn clean 和 mvn clean install的区别?[/color]
mvn clean只是清除当前项目中的target文件夹,mvn install是将jar(或其他包)入本地repo;而mvn clean install 等价于 mvn clean + mvn install。[quote][url]http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html[/url]
It should also be noted that the same command can be used in a multi-module scenario (i.e. a project with one or more subprojects). For example:
mvn clean install
This command will traverse into all of the subprojects and run clean, then install (including all of the prior steps).[/quote]
[color=red]maven默认打包哪些路径下的文件和文件夹?[/color]
在不指定打包类型的情况下(即运行mvn package),默认打包类型为jar(jar:jar - plugin:goal的格式),而jar:jar是只打target/classes目录下文件的:[quote][url]http://maven.apache.org/plugins/maven-jar-plugin/[/url]
jar:jar create a jar file for your project sources.
jar:test-jar create a jar file for your project test classes.
[url]http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html[/url]
[b]classesDirectory[/b]:Directory containing the classes and resource files that should be packaged into the JAR.
Default value is: ${project.build.outputDirectory}.[/quote]
[color=red]执行build的lifecycle中某个lifecycle的特定pahse,比如default lifecycle的compile phase(即:mvn compile),在这个lifecycle中位于当前执行的phase之前的phases,会自动执行????[/color]
是的!
[color=red]和mvn compile(即compile这个phase)绑定的compiler:compile是把/src/main/java和/src/main/recources下的文件都输出到/target/classes?[/color]
这个问题和上一个问题有直接联系!如果上一个问题答案是是,则这里的答案就应该是否!
mvn compile的最终结果是:将/src/main/java下的java源代码文件编译后,连同/src/main/recources中的资源文件一起(资源文件的拷贝工作是compile前的process-resources这个phase干的!),一起放如/target/classes输出目录下。
但单针对compiler:compile这个goal,它只是对/src/main/java下的java源代码文件进行编译,并输出到/target/classes目录下。


[color=red][b]settings.xml:[/b][/color]
Settings Reference:
[b][url]http://maven.apache.org/settings.html[/url][/b]
pom.xml or settings.xml? What is the best practice configuration usage for these files?
[url]http://www.javabeat.net/qna/750-pomxml-or-settingsxml-what-is-the-best-pra/[/url][quote]
The best practice guideline between settings.xml and pom.xml is that configurations in settings.xml must be specific to the current user and that pom.xml configurations are specific to the project.
For example, <repositories> in pom.xml would tell all users of the project to use the <repositories> specified in the pom.xml. However, some users may prefer to use a mirror instead, so they'll put <mirrors> in their settings.xml so they can choose a faster repository server.
so there you go:
settings.xml -> user scope
pom.xml -> project scope
[/quote][b]settings.xml 中为 maven 使用 http 代理 proxy:[/b]

<proxies>
<proxy>
<id>proxy-http</id>
<active>true</active>
<protocol>http</protocol>
<host>127.0.0.1</host>
<port>8087</port>
<!--<username>proxyuser</username>
<password>proxypass</password>-->
<nonProxyHosts>repository.xxx.com|some.host.com</nonProxyHosts>
</proxy>

<proxy>
<id>proxy-https</id>
<active>true</active>
<protocol>https</protocol>
<host>127.0.0.1</host>
<port>8087</port>
<!--<username>proxyuser</username>
<password>proxypass</password>-->
<nonProxyHosts>repository.xxx.com|some.host.com</nonProxyHosts>
</proxy>
</proxies>
[b]在 settings.xml 中使用 profile 为maven项目自动下载依赖的所有 jars 的 sources 和 javadoc:[/b]

<profiles>
<profile>
<id>downloadSources</id>
<properties>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</properties>
</profile>
</profiles>

<activeProfiles>
<activeProfile>downloadSources</activeProfile>
</activeProfiles>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值