maven中maven plugin 配置s和maven plugin 配置management的区别

这里给大家详细说一下Maven的运行机制,让大家不仅知其然,更知其所以然。
1.插件保存在哪里?
与我们所依赖的构件一样,插件也是基于坐标保存在我们的Maven仓库当中的。在用到插件的时候会先从本地仓库查找插件,如果本地仓库没有则从远程仓库查找插件并下载到本地仓库。
与普通的依赖构件不同的是,Maven会区别对待普通依赖的远程仓库与插件的远程仓库。前面提到的配置远程仓库只会对普通的依赖有效果。当Maven需要的插件在本地仓库不存在时是不会去我们以前配置的远程仓库查找插件的,而是需要有专门的插件远程仓库,我们来看看怎么配置插件远程仓库,在pom.xml加入如下内容:
1 &pluginRepositories&
&pluginRepository&
&id&nexus&/id&
&name&nexus&/name&
&url&http://192.168.0.70:8081/content/groups/public/&/url&
&releases&
&enabled&true&/enabled&
&/releases&
&snapshots&
<span style="color: #
&enabled&true&/enabled&
<span style="color: #
&/snapshots&
<span style="color: #
&/pluginRepository&
<span style="color: # &/pluginRepositories&
大家可以发现,除了pluginRepositories和pluginRepository与以前配置远程仓库不同以外,其他的都是一样的,所代表的含义也是一样的。Maven的父POM中也是有内置一个插件仓库的,我现在用的电脑安装的是Maven 3.0.4版本,我们可以找到这个文件:${M2_HOME}/lib/maven-model-builder-3.0.4.jar,打开该文件,能找到超级父POM:\org\apache\maven\model\pom-4.0.0.xml,它是所有Maven POM的父POM,所有Maven项目都继承该配置。
我们来看看默认的远程插件仓库配置的是啥:
1 &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&
<span style="color: #
&releases&
<span style="color: #
&updatePolicy&never&/updatePolicy&
<span style="color: #
&/releases&
<span style="color: #
&/pluginRepository&
<span style="color: # &/pluginRepositories&
默认插件仓库的地址就是中央仓库咯,它关闭了对snapshots的支持,防止引入snapshots版本的插件而导致不稳定的构件。一般来说,中央仓库所包含的插件完全能够满足我们的需要,只有在少数情况下才要配置,比如项目的插件无法在中央仓库找到,或者自己编写了插件才会配置自己的远程插件仓库。
2.插件命令运行解析
我们来看这样一个命令:
mvn compiler:compiler
这个命令会调用maven-compiler-plugin插件并执行compiler目标,大家有木有觉得很神奇?我们在pom.xml中配置插件往往是这样:
<span style="color: #
<span style="color: #
&groupId&org.apache.maven.plugins&/groupId&
<span style="color: #
&artifactId&maven-compiler-plugin&/artifactId&
<span style="color: #
&version&3.1&/version&
<span style="color: #
&configuration&
<span style="color: #
&source&1.7&/source& &!-- 源代码使用的开发版本 --&
<span style="color: #
&target&1.7&/target& &!-- 需要生成的目标class文件的编译版本 --&
<span style="color: #
&/configuration&
<span style="color: #
maven-compiler-plugin插件默认执行的目标为compiler,那么命令的完整写法应该是:mvn&org.apache.maven.plugins:maven-compiler-plugin:3.1:compiler才对啊,为什么mvn compiler:compiler也能完美的执行?
我们来看看Maven到底干了些神马来做到如此牛逼的功能:
①插件默认groupId
Maven默认以org.apache.maven.plugins作为groupId,到这里我们的命令应该是长这样的:
mvn&org.apache.maven.plugins:compiler:compiler
我们也可以配置自己默认的groupId,在Maven的settings.xml中添加如下内容,前面提过最好将settings.xml放在用户目录的.m2下:
<span style="color: # &pluginGroups&
<span style="color: #
&!-- pluginGroup
<span style="color: #
| Specifies a further group identifier to use for plugin lookup.
<span style="color: #
&pluginGroup&com.your.plugins&/pluginGroup&
<span style="color: #
<span style="color: #
&pluginGroup&com.your.plugins&/pluginGroup&
<span style="color: # &/pluginGroups&
不过说实在的,没必要动他就别去动他了,我们用Maven只是解决一些刚需的问题,没必要的设置就尽量不去动他,别把Maven搞得太复杂,虽然Maven的却有点小复杂,跟大家扯这些只是希望大家能够对maven理解的更深入那么一点点,并不是建议大家一定要去使用某些东西,大家在平时的开发中要谨记这一点。
②我们来看看Maven插件远程仓库的元数据org/apache/maven/plugins/maven-metadata.xml,Maven默认的远程仓库是,所有插件元数据路径则是:,我们找到compiler插件的元数据,如图:
这里会根据prefix指定的前缀找到对应的artifactId,到这里我们的命令应该长成了这样:
mvn&org.apache.maven.plugins:maven-compiler-plugin:compiler
③我们再根据groupId和artifactId找到maven-compiler-plugin插件单个的元数据,路径为,如图:
maven将所有的远程插件仓库及本地仓库元数据归并后,就能找到release的版本(maven3后为了保证项目构建的稳定性默认使用release版本),到这里命令就被扩展成为这样:
mvn&org.apache.maven.plugins:maven-compiler-plugin:3.6.0:compiler
如果执行的是mvn compiler:compiler命令,由于maven-compiler-plugin的最新版本已经到了3.6.0,则默认会使用此版本。最后的compiler则是插件要执行的目标咯,看到这里大家应该明白mvn compiler:compiler命令为什么能够得到完美的运行了吧。
3.Maven超级POM
最后给大家把超级父POM贴出来,再次强调,如果我们没有在自己的pom.xml中配置相应的内容,则默认会使用超级父POM配置的内容。我现在用的电脑安装的是Maven 3.0.4版本,我们可以找到这个文件:${M2_HOME}/lib/maven-model-builder-3.0.4.jar,打开该文件,能找到超级父POM:\org\apache\maven\model\pom-4.0.0.xml,它是所有Maven POM的父POM,所有Maven项目都继承该配置。
1 &?xml version="1.0" encoding="UTF-8"?&
4 Licensed to the Apache Software Foundation (ASF) under one
5 or more contributor license agreements.
See the NOTICE file
6 distributed with this work for additional information
7 regarding copyright ownership.
The ASF licenses this file
8 to you under the Apache License, Version 2.0 (the
9 "License"); you may not use this file except in compliance
10 with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing,
15 software distributed under the License is distributed on an
16 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 KIND, either express or implied.
See the License for the
18 specific language governing permissions and limitations
19 under the License.
22 &!-- START SNIPPET: superpom --&
23 &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&
&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-4453) --&
&artifactId&maven-antrun-plugin&/artifactId&
&version&1.3&/version&
&artifactId&maven-assembly-plugin&/artifactId&
&version&2.2-beta-5&/version&
&artifactId&maven-dependency-plugin&/artifactId&
&version&2.1&/version&
&artifactId&maven-release-plugin&/artifactId&
&version&2.0&/version&
&/plugins&
&/pluginManagement&
&reporting&
&outputDirectory&${project.build.directory}/site&/outputDirectory&
&/reporting&
&profiles&
<span style="color: #0
&!-- NOTE: The release profile will be removed from future versions of the super POM --&
<span style="color: #1
<span style="color: #2
&id&release-profile&/id&
<span style="color: #3
<span style="color: #4
&activation&
<span style="color: #5
&property&
<span style="color: #6
&name&performRelease&/name&
<span style="color: #7
&value&true&/value&
<span style="color: #8
&/property&
<span style="color: #9
&/activation&
<span style="color: #0
<span style="color: #1
<span style="color: #2
<span style="color: #3
<span style="color: #4
&inherited&true&/inherited&
<span style="color: #5
&artifactId&maven-source-plugin&/artifactId&
<span style="color: #6
&executions&
<span style="color: #7
&execution&
<span style="color: #8
&id&attach-sources&/id&
<span style="color: #9
<span style="color: #0
&goal&jar&/goal&
<span style="color: #1
<span style="color: #2
&/execution&
<span style="color: #3
&/executions&
<span style="color: #4
<span style="color: #5
<span style="color: #6
&inherited&true&/inherited&
<span style="color: #7
&artifactId&maven-javadoc-plugin&/artifactId&
<span style="color: #8
&executions&
<span style="color: #9
&execution&
<span style="color: #0
&id&attach-javadocs&/id&
<span style="color: #1
<span style="color: #2
&goal&jar&/goal&
<span style="color: #3
<span style="color: #4
&/execution&
<span style="color: #5
&/executions&
<span style="color: #6
<span style="color: #7
<span style="color: #8
&inherited&true&/inherited&
<span style="color: #9
&artifactId&maven-deploy-plugin&/artifactId&
<span style="color: #0
&configuration&
<span style="color: #1
&updateReleaseInfo&true&/updateReleaseInfo&
<span style="color: #2
&/configuration&
<span style="color: #3
<span style="color: #4
&/plugins&
<span style="color: #5
<span style="color: #6
&/profile&
<span style="color: #7
&/profiles&
<span style="color: #8
<span style="color: #9 &/project&
<span style="color: #0 &!-- END SNIPPET: superpom --&
很多插件是超级父POM当中并没有配置的,如果用户使用某个插件时没有设定版本,那么则会根据我上述所说的规则去仓库中查找可用的版本,然后做出选择。在Maven2中,插件的版本会被解析至latest。也就是说,当用户使用某个非核心插件且没有声明版本的时候,Maven会将版本解析为所有可用仓库中的最新版本,latest表示的就是最新版本,而这个版本很有可能是快照版本。
当插件为快照版本时,就会出现潜在的问题。昨天还好好的,可能今天就出错了,其原因是这个快照版本发生了变化导致的。为了防止这类问题,Maven3调整了解析机制,当插件没有声明版本的时候,不再解析至latest,而是使用release。这样就避免了由于快照频繁更新而导致的不稳定问题。但是这样就好了吗?不写版本号其实是不推荐的做法,例如,我使用的插件发布了一个新版本,而这个release版本与之前的版本的行为发生了变化,这种变化依然可能导致我们项目的瘫痪。所以使用插件的时候,应该一直显式的设定版本,这也解释了Maven为什么要在超级父POM中为核心插件设定版本咯。
结束语:当你感到悲哀痛苦时,最好是去学些什么东西,学习会使你从悲哀痛苦中走出来,学习会使你永远立于不败之地。说实在的,不要太在意眼前所发生的一切,更重要的是培养自己的个人能力,如今待在公司亦或者跳槽,决定你能不能继续走下去的一定是你的个人能力,作为年轻人,在公司更看重的不应该是薪水的高低,而是公司能给你带来多大的成长环境。找个好的公司其实不比找个合适的女朋友简单,作为年轻人我们一定要不断的提升个人能力,就跟找女朋友似的,往往就是你越有本事就越能够不将就,你个人能力越强则越有选择公司的资本。
可爱博主:AlanLee
博客地址:
本文出自博客园,欢迎大家加入博客园。
阅读(...) 评论()maven:pluginManagement标签的相关错误问题? - 知乎1被浏览155分享邀请回答还没有回答maven插件详解(查询maven插件并且使用插件的目标网站:http://maven.apache.org/plugins/ , http://search.maven.org/)
&&&&& 1:插件是maven的核心,所有执行的操作都是基于插件来完成的
&&& 为了让一个插件中可以实现众多的类&#20284;功能,maven为插件设定了目标,一个插件中有可能有多个目标
&& & 其实生命周期中的重要的每个阶段都是由插件的一个具体目标来执行的
&&&&&& 2:maven自己的package命令只能打包编译后的源代码,如果我们想打包源代码的话,就需要使用maven的插件来完成:
&&&&& 使用插件的步骤为:
&&&&&&& 1: 在pom中配置插件,示例如下:
&& &&&&&&&&&&&& &build&
&& &&& &&&& &plugins&
&& &&& &&& &&plugin&
&& &&& &&& &&& &&groupId&org.apache.maven.plugins&/groupId&
&& &&& &&& &&& &&artifactId&maven-source-plugin&/artifactId&
&& &&& &&& &&& &&version&2.1.2&/version&
&& &&& &&& &&/plugin&
&& &&& &&&& &/plugins&
&& &&&&&&&&&&& &/build&&&&&&&&&&&&&&&&
&& &&& &2: 我们也可以实现插件的继承依赖:在parent项目中配置如下:
&& &&& &&&&&&&&& &build&
&& &&& &&& &&& &&pluginManagement&
&& &&& &&& &&& &&&& &plugins&
&& &&& &&& &&& &&&&&&& &plugin&
&& &&& &&& &&& &&& &&& &groupId&org.apache.maven.plugins&/groupId&
&& &&& &&& &&& &&&&&&&&&&& &artifactId&maven-source-plugin&/artifactId&
&& &&& &&& &&& &&&&&&&&&&& &version&2.1.2&/version&
&& &&& &&& &&& &&&&&&& &/plugin&
&& &&& &&& &&& &&&& &/plugins&
&& &&& &&& &&& &&/pluginManagement&
&& &&& &&& &&& &/build&
&& &&& &&& &&& 子项目的配置:
&& &&& &&& &&& &build&
&& &&& &&& &&& &&plugins&
&& &&& &&& &&& &&& &&plugin&
&& &&& &&& &&& &&& &&& &&groupId&org.apache.maven.plugins&/groupId&
&& &&& &&& &&& &&& &&& &&artifactId&maven-source-plugin&/artifactId&
&& &&& &&& &&& &&& &&/plugin&
&& &&& &&& &&& &&/plugins&
&& &&& &&& &&&& &/build&
&&&&&&&&&&&&&&&&&& &&&&&
&&&&&& 3: 将插件绑定在一个特定的生命周期上,如果我们不设置,会默认绑定到一个生命周期上的,使用svn将源代码checkout下来,查看源代码即可看到默认&#20540;
&&&&&&&&&& &build&
&& &&&&&&&& &pluginManagement&
&& &&&&&&&&&&&& &plugins&
&& &&&&&&&&&&&&&&& &plugin&
&& &&&&&&&&&&&&&&&&&&& &groupId&org.apache.maven.plugins&/groupId&
&& &&& &&& &&& &&&&&&& &artifactId&maven-source-plugin&/artifactId&
&& &&& &&& &&& &&&&&&& &version&2.1.2&/version&
&& &&& &&& &&& &&&&&&& &executions&
&& &&& &&& &&& &&&&&&&&&&& &execution&
&& &&& &&& &&& &&&&&&&&&&&&& &phase&package&/phase&
&& &&& &&& &&& &&&&&&&&&&&&& &goals&&goal&jar-no-fork&/goal&&/goals&
&& &&& &&& &&& &&&&&&&&&&& &/execution&
&& &&& &&& &&& &&&&&&& &/executions&
&& &&&&&&&&&&&&&&& &/plugin&
&& &&&&&&&&&&&& &/plugins&
&& &&&&&&&& &/pluginManagement&
&& &&& &/build&
&& &&& 以上就表示我们执行完package生命周期后就执行我们的maven-source-plugin插件中的jar-no-fork目标
&& &&& 这样就将我们的插件的的目标执行绑定到了特定的生命周期上,这是在父项目中编写的,子项目直接继承即可
&&&&& 4:maven-help-plugin插件: 显示我们想要显示的帮助信息
&&&&&&&&&&&&& help:describe -Dplugin=compiler,compiler代表的是我们要显示信息的插件的前缀
&& &&&&&& 或者:help:describe -Dmojo=describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-help-plugin
&&&&& 5:如果我们使用的某些插件需要设定一些参数那么就需要使用configuration来配置:
&&&&&&&&&&&&&&&&&&&&&& &plugin&
&& &&& &&& &&& &&groupId&org.apache.maven.plugins&/groupId&
&& &&& &&& &&& &&artifactId&maven-source-plugin&/artifactId&
&& &&& &&& &
&& &&& &&& & &configuration&
&& &&& &&& &&&&&& 这里会自动提示我们能编写的属性,在编写属性的时候建议先将属性定义
&& &&& &&& & &/configuration&
&& &&& &&& &
&& &&& &&& &&/plugin&
&& && sql-maven-plugin插件的使用:
&& &&&&&&&&&&&&&&&& &plugin&
& && &&& &&& &&& &&groupId&org.codehaus.mojo&/groupId&
&& &&& &&& &&& &&artifactId&sql-maven-plugin&/artifactId&
&& &&& &&& &&& &&version&1.5&/version&
&&&&& &&& &&& &&& &&dependencies&
&&&&& &&& &&& &&& &&& &&dependency&
&&&&& &&& &&& &&& &&& &&& &&groupId&mysql&/groupId&
&& &&& &&& &&& &&&&&&&& &artifactId&mysql-connector-java&/artifactId&
&& &&& &&& &&& &&&&&&&& &version&5.1.18&/version&
&&&&& &&& &&& &&& &&& &&/dependency&
&&&&& &&& &&& &&& &&/dependencies&
&&&&& &&& &&& &&& &&configuration&
&&&&& &&& &&& &&& &&& && &driver&${mysql.driver}&/driver&
&& &&& &&& &&&&&&&&&& &url&${mysql.url}&/url&
&& &&& &&& &&&&&&&&&& &username&${mysql.username}&/username&
&& &&& &&& &&&&&&&&&& &password&${mysql.password}&/password&
&& &&& &&& &&&&&&&&&& &sqlCommand&
&& &&& &&& &&&&&&&&& &&& &create database IF NOT EXISTS maven_test
&& &&& &&& &&&&&&&&&& &/sqlCommand&
&&&&& &&& &&& &&& &&/configuration&
&&&&& &&& &&& &&& &
&&&&& &&& &&& &&& &&executions&
&&&&& &&& &&& &&& &&& &&execution&
&&&&& &&& &&& &&& &&& &&& &&phase&package&/phase&
&&&&& &&& &&& &&& &&& &&& &&goals&
&&&&& &&& &&& &&& &&& &&& && &goal&execute&/goal&
&&&&& &&& &&& &&& &&& &&& &&/goals&
&&&&& &&& &&& &&& &&& &&/execution&
&&&&& &&& &&& &&& &&/executions&
& && &&& &&& &&/plugin&
&&&&& 6 : & 插件使用的基本步骤: 配置GVA-----绑定到生命周期(executions)-------配置执行的参数(configuration)----------如果插件的执行需要依赖,那么我们通过&dependencies&来引入依赖
本文已收录于以下专栏:
相关文章推荐
先将我用到的自定义变量贴过来,防止插件中出现一些变量,大家不理解&#160;&#160;&#160;
Java代码&#160;&#160;&#160;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;tools&#160;&#1...
AntRun插件是为了便于从Ant项目迁移到Maven而设计的,提供了在Maven中运行Ant target的能力。当前最新版本是2014.12发布的1.8。
甚至,可以将Ant脚本直接嵌入到POM中...
Maven是一个优秀的项目管理工具,它能够帮你管理编译、报告、文档等。
Maven的生命周期:
maven的生命周期是抽象的,它本身并不做任何的工作。实际的工作都交由&插件&来完成。
maven的每...
Ant&#160;插件用以生成Ant工具的build相关配置文件,要求Ant 1.6.2及以后版本。
Ant&#160;插件的最新版本是2014.12发布的2.4
Ant插件的基本信息:
[html] view plai...
maven的插件解析原理和正确的插件配置方式。
Archetype插件是Apache Maven项目提供的一个工具类的通用插件。
Archetype插件可以基于已有的模板创建一个Maven项目,开发人员可以在此基础上进行扩展开发。在这里,项目...
Compiler插件用于将编译Maven项目的Java源代码,最新版本是2016.2发布的3.5.1。
Compiler插件提供了如下2个goal,默认都已经绑定到Maven的生命周期阶段,无需单...
Compiler插件用于编译Maven项目的Java源代码,最新版本是2016.2发布的3.5.1(刚刚看到,最新版本已经更新为3.6.0,更新)。
Compiler插件提供了如下...
&#160; 最近刚接触Maven,对Maven才了解些皮毛就被老大安排协助编译XWIKI源码,期间急需一款好的Web容器,并能有很好的Maven插件支持及debug功能,可能jetty[注解1]是很...
(声明:本文是学习了大牛徐晓斌的Maven3 in Action之后的总结,仅用于学习交流。)插件与插件目标Maven定义了三套相互独立的生命周期,每套生命周期都有多个生命周期阶段,而这些阶段都是抽象...
他的最新文章
讲师:汪剑
讲师:刘道宽
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 maven antrun plugin 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信