当前位置: 首页 > news >正文

网站建设维护兼职梅州正规的免费建站

网站建设维护兼职,梅州正规的免费建站,专题页网站怎么做,如何给国外网站做seoMaven实战-声明周期和插件 Maven 设计了插件机制#xff0c;每个构建步骤都可以绑定一个或者多个插件行为#xff0c;而且 Maven 为大多数构建步骤编写 并绑定了默认插件。例如#xff0c;针对编译的插件有 maven-compiler-plugin#xff0c;针对测试的插件有 maven-sure…Maven实战-声明周期和插件 Maven 设计了插件机制每个构建步骤都可以绑定一个或者多个插件行为而且 Maven 为大多数构建步骤编写 并绑定了默认插件。例如针对编译的插件有 maven-compiler-plugin针对测试的插件有 maven-surefire- plugin等。虽然在大多数时间里用户几乎都不会觉察到插件的存在但实际上编译是由 maven-compiler-plugin 完成的而测试是由 maven-surefire-plugin 完成的。 当用户有特殊需要的时候也可以配置插件定制构建行 为甚至自己编写插件。 Maven 定义的生命周期和插件机制一方面保证了所有 Maven 项目有一致的构建标准另一方面又通过默认插件 简化和稳定了实际项目的构建。此外该机制还提供了足够的扩展空间用户可以通过配置现有插件或者自行编写 插件来自定义构建行为。 1、声明周期详解 1.1 三套声明周期 Maven 拥有三套相互独立的生命周期它们分别为 clean、default 和 site。clean 生命周期的目的是清理项 目default 生命周期的目的是构建项目而 site 生命周期的目的是建立项目站点。 每个生命周期包含一些阶段(phase)这些阶段是有顺序的并且后面的阶段依赖于前面的阶段用户和 Maven 最直接的交互方式就是调用这些生命周期阶段。以 clean 生命周期为例它包含的阶段有 pre-clean、clean 和 post-clean。 当用户调用 pre-clean 的时候只有 pre-clean 阶段得以执行当用户调用clean的时候pre-clean 和 clean 阶段会得以顺序执行当用户调用 post-clean 的时候pre-clean、clean和 post-clean 会得以顺序执 行。 较之于生命周期阶段的前后依赖关系三套生命周期本身是相互独立的用户可以仅仅调用clean生命周期的某个 阶段或者仅仅调用default生命周期的某个阶段而不会对其他生命周期产生任何影响。例如当用户调用 clean生命周期的 clean 阶段的时候不会触发default生命周期的任何阶段反之亦然当用户调用default生命 周期的 compile 阶段的时候也不会触发clean生命周期的任何阶段。 1.2 clean声明周期 clean 声明周期的目的是清理项目它包含是三个阶段 pre-clean执行一些清理前需要完成的工作。 clean清理上一次构建生成的文件。 post-clean执行一些清理后需要完成的工作 1.3 default声明周期 default声明周期定义了真正构建时所需要执行的所有步骤它是生命周期中最核心的部分其包含的阶段如下 validate initialize generate-sources process-sources处理项目主资源文件。一般来说是对src/main/resources目录的内容进行变量替换等工 作后复制到项目输出的主classpath目录中。 generate-resources process-resources compile编译项目的主源码。一般来说是编译src/main/java目录下的Java文件至项目输出的主classpath 目录中。 process-classes generate-test-sources process-test-sources处理项目测试资源文件。一般来说是对src/test/resources目录的内容进行变量替换 等工作后复制到项目输出的测试classpath目录中。 generate-test-resources process-test-resources test-compile编译项目的测试代码一般来说是编译src/test/java目录下的Java文件至项目输出的测试 classpath目录中。 process-test-classes test使用单元测试框架运行测试测试代码不会打包或部署。 prepare-package package接受编译好的代码打包成可发布的格式如JAR。 pre-integration-test integration-test post-integration-test verify install将包安装到Maven本地仓库供本地其他Maven项目使用。 deploy将最终的包复制到远程仓库供其他开发人员和Maven项目使用。 官方文档 http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html1.4 site声明周期 site生命周期的目的是建立和发布项目站点Maven 能够基于POM所包含的信息自动生成一个友好的站点方 便团队交流和发布项目信息。该声明周期包含如下阶段 pre-site执行一些在生成项目站点之前需要完成的工作。 site生成项目站点文档。 post-site执行一些在生成项目站点之后需要完成的工作。 site-deploy将生成的项目站点发布到服务器上。 1.5 命令行与生命周期 从命令行执行 Maven 任务的最主要方式就是调用 Maven 的生命周期阶段。需要注意的是各个生命周期是相互 独立的而一个生命周期的阶段是有前后依赖关系的。下面以一些常见的 Maven命令为例解释其执行的生命周 期阶段 $mvn clean该命令调用clean生命周期的 clean 阶段。实际执行的阶段为 clean生命周期的 pre-clean 和 clean 阶段。 $mvn test该命令调用 default生命周期的 test 阶段。实际执行的阶段为 default 生命周期的 validate、 initialize 等直到test的所有阶段。这也解释了为什么在执行测试的时候项目的代码能够自动得以编译。 $mvn clean install该命令调用 clean生命周期的clean 阶段和 default生命周期的install 阶段。实际执 行的阶段为 clean 生命周期的 pre-clean、clean 阶段以及 default 生命周期的从 validate至 install 的所有 阶段。该命令结合了两个生命周期在执行真正的项目构建之前清理项目是一个很好的实践。 $mvn clean deploy site-deploy该命令调用clean生命周期的clean阶段、default生命周期的 deploy 阶 段以及 site 生命周期的 site-deploy 阶段。实际执行的阶段为clean生命周期的 pre-clean、clean 阶段 default 生命周期的所有阶段以及 site 生命周期的所有阶段。该命令结合了 Maven 所有三个生命周期且 deploy 为default生命周期的最后一个阶段site-deploy 为 site生命周期的最后一个阶段。 由于Maven 中主要的生命周期阶段并不多而常用的 Maven 命令实际都是基于这些阶段简单组合而成的因此 只要对 Maven 生命周期有一个基本的理解读者就可以正确而熟练地使用 Maven 命令。 2、插件目标 在进一步详述插件和生命周期的绑定关系之前必须先了解插件目标(Plugin Goal)的概念。我们知道Maven 的 核心仅仅定义了抽象的生命周期具体的任务是交由插件完成的插件以独立的构件形式存在因此Maven 核 心的分发包只有不到3MB的大小Maven 会在需要的时候下载并使用插件。 对于插件本身为了能够复用代码它往往能够完成多个任务。例如 maven-dependency-plugin它能够基于项 目依赖做很多事情。它能够分析项目依赖帮助找出潜在的无用依赖它能够列出项目的依赖树帮助分析依赖来 源它能够列出项目所有已解析的依赖等等。为每个这样的功能编写一个独立的插件显然是不可取的因为这些 任务背后有很多可以复用的代码因此这些功能聚集在一个插件里每个功能就是一个插件目标。 maven-dependency-plugin 有十多个目标每个目标对应了一个功能上述提到的几个功能分别对应的插件目标 为 dependency:analyze、dependency:tree 和 dependency:list。这是一种通用的写法冒号前面是插件前缀 冒号后面是该插件的目标。类似地还可以写出 compiler:compile(这是 maven-compiler-plugin 的 compile 目 标)和 surefire:test(这是 maven-surefire-plugin 的 test 目标)。 3、插件绑定 Maven 的生命周期与插件相互绑定用以完成实际的构建任务。具体而言是生命周期的阶段与插件的目标相互 绑定以完成某个具体的构建任务。例如项目编译这一任务它对应了 default生命周期的 compile 这一阶段而 maven-compiler-plugin这一插件的 compile目标能够完成该任务。因此将它们绑定就能实现项目编译的目 的。 3.1 内容绑定 为了能让用户几乎不用任何配置就能构建 Maven 项目Maven 在核心为一些主要的生命周期阶段绑定了很多插 件的目标当用户通过命令行调用生命周期阶段的时候对应的插件目标就会执行相应的任务。 clean生命周期仅有 pre-clean、clean 和 post-clean 三个阶段其中的clean与maven-clean-plugin:clean 绑定。 maven-clean-plugin 仅有 clean这一个目标其作用就是删除项目的输出目录。clean生命周期阶段与插件目标的 绑定关系如下表所示。 生命周期阶段插件目标pre-cleancleanmaven-clean-plugin:cleanpost-clean site 生命周期有 pre-site、site,post-site 和 site-deploy 四个阶段其中site 和maven-site-plugin:site 相互绑 定site-deploy 和 maven-site-plugin:depoy 相互绑定。maven-site-plugin 有很多目标其中site 目标用来生 成项目站点deploy目标用来将项目站点部署到远程服务器上。site生命周期阶段与插件目标的绑定关系如下表所 示。 生命周期阶段插件目标pre-sitesitemaven-site-plugin:sitepost-sitesite-deploymaven-site-plugin:deploy 相对于clean和site生命周期来说default生命周期与插件目标的绑定系就显得复杂一些。这是因为对于任何项目 来说例如jar项目和war项目它们的项目清理和站点生成任务是一样的不过构建过程会有区别。例如jar 项目 需要打成JAR 包而war 项目需要打成WAR包。 由于项目的打包类型会影响构建的具体过程因此default生命周期的阶段与插件目标的绑定关系由项目打包类 型所决定打包类型是通过POM中的 packaging元素定义的。最常见、最重要的打包类型是jar它也是默认的打 包类型。基于该打包类型的项目其 default 生命周期的内置插件绑定关系及具体任务如下表所示。 生命周期阶段插件目标执行任务process-resourcesmaven-resources-plugin:resources复制主资源文件至主输出目录compilemaven-compile-plugin:compile编译主代码至主输出目录process-test-resourcesmaven-resources-plugin:testRresources复制测试资源文件至测试输出目录test-compilemaven-compiler-plugin:testCompile编译测试代码至测试输出目录testmaven-surefire-plugin:test执行测试用例packagemaven-jar-plugin:jar创建项目jar包installmaven-install-plugin:install将项目输出构件安装到本地仓库deploymaven-deploy-plugin:deploy将项目输出构件部署到远程仓库 注意上表只列出了拥有插件绑定关系的阶段default 生命周期还有很多其他阶段默认它们没有绑定任何插 件因此也没有任何实际行为。 除了默认的打包类型 jar 之外常见的打包类型还有war、pom、maven-plugin、ear 等。 它们的 default 生命周期与插件目标的绑定关系可参阅 Maven 官方文档 https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Built-in_Lifecycle_Bind-ings读者可以从Maven的命令行输出中看到在项目构建过程执行了哪些插件目标例如执行 mvn clean install 命令 可以看到如下输出 $ mvn clean install [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) hello-maven --- [INFO] Deleting D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) hello-maven --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\ta rget\classes [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\Hell oMaven\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) hello-maven --- [INFO] Changes detected - recompiling the module! [WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent! [INFO] Compiling 1 source file to D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\ta rget\test-classes [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) hello-maven --- [INFO] Surefire report directory: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\ta rget\surefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running org.apache.HelloWorldTest init Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.053 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 1[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) hello-maven --- [INFO] Building jar: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-ma ven-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-shade-plugin:3.2.4:shade (default) hello-maven --- [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven- 1.0-SNAPSHOT.jar with D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-m aven-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) hello-maven --- [INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven -1.0-SNAPSHOT.jar to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHO T.jar [INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\pom.xml to D:\Deco mpressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.pom [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.194 s [INFO] Finished at: 2023-10-15T11:22:2308:00 [INFO] Final Memory: 21M/310M [INFO] ------------------------------------------------------------------------从输出中可以看到执行的插件目标依次为 maven-clean-plugin:clean、 maven-resources-plugin:resources、maven-compiler-plugin:compile、 maven-resources-plugin:testResources、maven-compiler-plugin:testCompile、 maven-surefire-plugin:test、maven-jar-plugin:jar 和 maven-install-plugin:install。 我们知道mvn clean install 令实际调用了 clean 生命周期的 pre-clean、clean 阶段以及default生命周期的从 validate至install 所有阶段。在此基础上通过对照上面的几个表就能从理论上得到将会执行的插件目标任务 而实际的输出完全验证了这一点。 3.2 自定义绑定 除了内置绑定以外用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上这种自定义绑定方式能让 Maven 项目在构建过程中执行更多更富特色的任务。 一个常见的例子是创建项目的源码jar包内置的插件绑定关系中并没有涉及这一任务因此需要用户自行配置。 maven-source-plugin 可以帮助我们完成该任务它的jar-no-fork目标能够将项目的主代码打包成jar文件可以 将其绑定到 default生命周期的 verify 阶段上在执行完集成测试后和安装构件之前创建源码 jar包。具体配置见 代码 plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-source-plugin/artifactIdversion3.2.1/version!-- executions下每个execution子元素可以用来配置执行一个任务该例中配置了一个id为attach-sources的任务,通过phase配置,将其绑定到verify生命周期阶段上再通过goals配置指定要执行的插件目标,至此,自定义插件绑定完成--executionsexecutionidattach-sources/idphaseverify/phasegoalsgoaljar-no-fork/goal/goals/execution/executions /plugin运行 mvn verify 就能看到如下输出 $ mvn verify [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) hello-maven --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\Hell oMaven\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) hello-maven --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) hello-maven --- [INFO] Surefire report directory: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\ta rget\surefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running org.apache.HelloWorldTest init Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.053 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 1[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) hello-maven --- [INFO] Building jar: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-ma ven-1.0-SNAPSHOT.jar [INFO] [INFO] --- maven-shade-plugin:3.2.4:shade (default) hello-maven --- [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven- 1.0-SNAPSHOT.jar with D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-m aven-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-sources) hello-maven --- [INFO] Building jar: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-ma ven-1.0-SNAPSHOT-sources.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.007 s [INFO] Finished at: 2023-10-15T11:38:5708:00 [INFO] Final Memory: 13M/245M [INFO] ------------------------------------------------------------------------我们可以看到当执行 verify生命周期阶段的时候maven-source-plugin:jar-no-fork 会得以执行它会创建一 个以 -sources.jar 结尾的源码文件包。 有时候即使不通过 phase 元素配置生命周期阶段插件目标也能够绑定到生命周期中去。例如可以尝试删除 上述配置中的phase一行再次执行 mvn verify仍然可以看到 maven-source-plugin:jar-no-fork 得以执行。出 现这种现象的原因是有很多插件的目标在编写时已经定义了默认绑定阶段。可以使用 maven-help-plugin 查看 插件详细信息了解插件目标的默认绑定阶段。运行命令如下 $ mvn help:describe -Dpluginorg.apache.maven.plugins:maven-source-plugin:3.2.1 -Ddetail该命令输出对应插件的详细信息。在输出信息中能够看到关于目标 jar-no-fork 的如下信息 source:jar-no-forkDescription: This goal bundles all the sources into a jar archive. Thisgoal functions the same as the jar goal but does not fork the build and issuitable for attaching to the build lifecycle.Implementation: org.apache.maven.plugin.source.SourceJarNoForkMojoLanguage: javaBound to phase: packageAvailable parameters:archiveThe archive configuration to use. See Maven Archiver Reference.attach (Default: true)User property: attachSpecifies whether or not to attach the artifact to the projectexcludeResources (Default: false)User property: source.excludeResourcesSpecifies whether or not to exclude resources from the sources-jar. Thiscan be convenient if your project includes large resources, such asimages, and you dont want to include them in the sources-jar.excludesList of files to exclude. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.finalName (Default: ${project.build.finalName})The filename to be used for the generated archive file. For thesource:jar goal, -sources is appended to this filename. For thesource:test-jar goal, -test-sources is appended.forceCreation (Default: false)User property: source.forceCreationWhether creating the archive should be forced. If set to true, the jarwill always be created. If set to false, the jar will only be createdwhen the sources are newer than the jar.includePom (Default: false)User property: source.includePomSpecifies whether or not to include the POM file in the sources-jar.includesList of files to include. Specified as fileset patterns which arerelative to the input directory whose contents is being packaged into theJAR.outputDirectory (Default: ${project.build.directory})The directory where the generated archive file will be put.useDefaultExcludes (Default: true)Exclude commonly excluded files such as SCM configuration. These aredefined in the plexus FileUtils.getDefaultExcludes()useDefaultManifestFile (Default: false)Set this to true to enable the use of the defaultManifestFile.该输出包含了一段关于 jar-no-fork 目标的描述这里关心的是 Bound to phase 这一项它表示该目标默认绑定 的生命周期阶段(这里是 package)。也就是说当用户配置使用 maven-source-plugin 的 jar-no-fork 目标的时 候如果不指定 phase 参数该目标就会被绑定到 package 阶段。 我们知道当插件目标被绑定到不同的生命周期阶段的时候其执行顺序会由生命周期阶段的先后顺序决定。如果 多个目标被绑定到同一个阶段它们的执行顺序会是怎样? 答案很简单当多个插件目标绑定到同一个阶段的时候这些插件声明的先后顺序决定了目标的执行顺序。 4、插件配置 完成了插件和生命周期的绑定之后用户还可以配置插件目标的参数进一步调整插件目标所执行的任务以满足 项目的需求。几乎所有 Maven 插件的目标都有一些可配置的参数用户可以通过命令行和 POM 配置等方式来配 置这些参数。 4.4.1 命令行插件配置 在日常的 Maven 使用中我们会经常从命令行输入并执行 Maven 命令。在这种情况下如果能够方便地更改某 些插件的行为无疑会十分方便。很多插件目标的参数都支持从命令行配置用户可以在 Maven 命令中使用 -D 参数并伴随一个参数键参数值的形式来配置插件目标的参数。 例如maven-surefire-plugin 提供了一个 maven.test.skip 参数当其值为 true 的时候就会跳过执行测试。于 是在运行命令的时候,加上如下 -D 参数就能跳过测试 $ mvn install -Dmaven.test.skiptrue [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) hello-maven --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) hello-maven --- [INFO] Not copying test resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) hello-maven --- [INFO] Not compiling test sources [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) hello-maven --- [INFO] Tests are skipped. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) hello-maven --- [INFO] [INFO] --- maven-shade-plugin:3.2.4:shade (default) hello-maven --- [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven-1.0-SNAPSHOT.jar with D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-sources) hello-maven --- [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) hello-maven --- [INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven-1.0-SNAPSHOT.jar to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.jar [INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\pom.xml to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT.pom [INFO] Installing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven-1.0-SNAPSHOT-sources.jar to D:\DecompressionSoftwareInstall\apache-maven-3.3.9-bin\repository\org\example\hello-maven\1.0-SNAPSHOT\hello-maven-1.0-SNAPSHOT-sources.jar [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.161 s [INFO] Finished at: 2023-10-15T13:36:0208:00 [INFO] Final Memory: 14M/309M [INFO] ------------------------------------------------------------------------参数 -D 是 Java 自带的其功能是通过命令行设置一个Java 系统属性Maven 简单地重用了该参数在准备插件 的时候检查系统属性便实现了插件参数的配置。 4.4.2 POM中插件全局配置 并不是所有的插件参数都适合从命令行配置有些参数的值从项目创建到项目发布都不会改变或者说很少改变 对于这种情况在POM文件中一次性配置就显然比重复在命令行输人要方便。 用户可以在声明插件的时候对此插件进行一个全局的配置。也就是说所有该基于该插件目标的任务都会使用 这些配置。例如我们通常会需要配置 maven-compiler-plugin 告诉它编译 Java 1.8 版本的源文件生成与 JVM1.8 兼容的字节码文件。 plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.10.1/versionconfigurationsource1.8/sourcetarget1.8/target/configuration /plugin这样不管绑定到 compile 阶段的 maven-compiler-plugin:compile 任务还是绑定到test-compiler 阶段的 maven-compiler-plugin:testCompiler 任务就都能够使用该配置基于 Java 1.5 版本进行编译。 4.4.3 POM 中插件任务配置 除了为插件配置全局的参数用户还可以为某个插件任务配置特定的参数。以 maven-antrun-plugin 为例它有 一个目标run可以用来在 Maven 中调用 Ant 任务。用户将 maven-antrun-plugin:run 绑定到多个生命周期阶段 上再加以不同的配置就可以让 Maven 在不同的生命阶段执行不同的任务。 plugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-antrun-plugin/artifactIdversion1.8/versionexecutionsexecutionidant-validate/idphasevalidate/phasegoalsgoalrun/goal/goalsconfigurationtasksechoIm bound to validate phase./echo/tasks/configuration/executionexecutionidant-verify/idphaseverify/phasegoalsgoalrun/goal/goalsconfigurationtasksechoIm bound to verify phase./echo/tasks/configuration/execution/executions /plugin在上述代码片段中首先maven-antrun-plugin:run 与 validate 阶段绑定从而构成一个 id 为 ant-validate 的任务。插件全局配置中的 configuration元素位于 plugin元素下面而这里的 configuration 元素则位于 execution元素下表示这是特定任务的配置而非插件整体的配置。这个ant-validate任务配置了一个echo Ant 任务向命令行输出一段文字表示该任务是绑定到 validate 阶段的。第二个任务的id为 ant-verify它绑定到了 verify 阶段同样它也输出一段文字到命令行告诉该任务绑定到了 verify 阶段。 $ mvn verify [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-antrun-plugin:1.8:run (ant-validate) hello-maven --- [WARNING] Parameter tasks is deprecated, use target instead [INFO] Executing tasksmain:[echo] Im bound to validate phase. [INFO] Executed tasks [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) hello-maven --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) hello-maven --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\src\test\resources [INFO] [INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) hello-maven --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) hello-maven --- [INFO] Surefire report directory: D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\surefire-reports-------------------------------------------------------T E S T S ------------------------------------------------------- Running org.apache.HelloWorldTest init Tests run: 2, Failures: 0, Errors: 0, Skipped: 1, Time elapsed: 0.047 secResults :Tests run: 2, Failures: 0, Errors: 0, Skipped: 1[INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) hello-maven --- [INFO] [INFO] --- maven-shade-plugin:3.2.4:shade (default) hello-maven --- [INFO] Replacing original artifact with shaded artifact. [INFO] Replacing D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven-1.0-SNAPSHOT.jar with D:\zhangshixing\hexo\website-hexo-theme-particlex\source\_posts\Maven\code\Maven实战-声明周期和插件\HelloMaven\target\hello-maven-1.0-SNAPSHOT-shaded.jar [INFO] [INFO] --- maven-source-plugin:3.2.1:jar-no-fork (attach-sources) hello-maven --- [INFO] [INFO] --- maven-antrun-plugin:1.8:run (ant-verify) hello-maven --- [WARNING] Parameter tasks is deprecated, use target instead [INFO] Executing tasksmain:[echo] Im bound to verify phase. [INFO] Executed tasks [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.442 s [INFO] Finished at: 2023-10-15T13:45:3308:00 [INFO] Final Memory: 15M/215M [INFO] ------------------------------------------------------------------------4.5 获取插件信息 仅仅理解如何配置使用插件是不够的。当遇到一个构建任务的时候用户还需要知道去哪里寻找合适的插件以帮 助完成任务。找到正确的插件之后还要详细了解该插件的配置点。由于Maven 的插件非常多而且这其中的大 部分没有完善的文档因此使用正确的插件并进行正确的配置其实并不是一件容易的事。 4.5.1 在线插件信息 基本上所有主要的 Maven 插件都来自 Apache 和 Codehaus。由于 Maven 本身是属于 Apache 软件基金会的 因此它有很多官方的插件每天都有成千上万的 Maven 用户在使用这些插件它们具有非常好的稳定性。详细的 列表可以在这个地址得到https://maven.apache.org/plugins/index.html 单击某个插件的链接便可以得到进一步的信息。 所有官方插件能在这里下载https://repo1.maven.org/maven2/org/apache/maven/plugins/。 除了 Apache 上的官方插件之外托管于 Codehaus上的 Mojo 项目也提供了大量了 Maven插件详细的列表可 以访问http://mojo.codehaus.org/plugins.html。需要注意的是这些插件的文档和可靠性相对较差在 使用时如果遇到问题,往往只能自己去看源代码。所有 Codehaus 的 Maven 插件能在这里下载 http://repository.codehaus.org/org/codehaus/mojo/ 由于上述两个站点提供的插件非常多而实际使用中常用的插件远不会是这个数量因此附录归纳了一些比较常用 的插件。 虽然并非所有插件都提供了完善的文档但一些核心插件的文档还是非常丰富的。以 maven-surefire-plugin 为 例访问 http://maven.apache.org/plugins/maven-surefire-plugin/ 可以看到该插件的简要介绍、包含 的目标、使用介绍、FAQ 以及很多实例。 一般来说通过阅读插件文档中的使用介绍和实例就应该能够在自己的项目中很好地使用该插件。但当我们想了 解非常细节的目标参数时就需要进一步访问该插件每个目标的文档。以 maven-surefire-plugin 为例可以通过 在命令行传人 maven.test.skip 参数来跳过测试执行而执行测试的插件目标是 surefire:test访问其文档 http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html可以找到目标参数 skip。 文档详细解释了该参数的作用、类型等信息。基于该信息用户可以在POM中配置 maven-surefire-plugin 的 skip 参数为 true来跳过测试。这个时候读者可能会不理解了之前在命令行传入的参数不是 maven.test.skip吗? 的确如此虽然对于该插件目标的作用是一样的但从命令行传人的参数确实不同于该插件目标的参数名称。命令 行参数是由该插件参数的表达式(Expression)决定的。可以看到surefire:test skip参数的表达式为 ${maven.test.skip}它表示可以在命令行以 -Dmaven.test.skiptrue 的方式配置该目标。并不是所有 插件目标参数都有表达式也就是说一些插件目标参数只能在POM中配置。 4.5.2 使用maven-help-plugin描述插件 除了访问在线的插件文档之外还可以借助 maven-help-plugin 来获取插件的详细信息。 可以运行如下命令来获取 maven-compiler-plugin 3.10.1 版本的信息 $ mvn help:describe -Dpluginorg.apache.maven.plugins:maven-compiler-plugin:3.10.1 [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 109, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:3.3.0:describe (default-cli) hello-maven --- [INFO] org.apache.maven.plugins:maven-compiler-plugin:3.10.1Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of yourproject. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version: 3.10.1 Goal Prefix: compilerThis plugin has 3 goals:compiler:compileDescription: Compiles application sourcescompiler:helpDescription: Display help information on maven-compiler-plugin.Call mvn compiler:help -Ddetailtrue -Dgoalgoal-name to displayparameter details.compiler:testCompileDescription: Compiles application test sources.For more information, run mvn help:describe [...] -Ddetail[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.921 s [INFO] Finished at: 2023-10-15T17:17:1908:00 [INFO] Final Memory: 14M/309M [INFO] ------------------------------------------------------------------------这里执行的是 maven-help-plugin 的 describe 目标在参数 plugin 中输入需要描述插件的 groupId、artifactld 和 version。Maven 在命令行输出 maven-compiler-plugin 的简要信息包括该插件的坐标、目标前缀和目标 等。 Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of yourproject. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version: 3.10.1 Goal Prefix: compilerThis plugin has 3 goals:compiler:compileDescription: Compiles application sourcescompiler:helpDescription: Display help information on maven-compiler-plugin.Call mvn compiler:help -Ddetailtrue -Dgoalgoal-name to displayparameter details.compiler:testCompileDescription: Compiles application test sources.For more information, run mvn help:describe [...] -Ddetail对于坐标和插件目标不再多做解释这里值得一提的是目标前缀(Goal Prefix)其作用是方便在命令行直接运行 插件。maven-compiler-plugin 的目标前缀是 compiler。 在描述插件的时候还可以省去版本信息让Maven 自动获取最新版本来进行表述。 例如 $ mvn help:describe -Dpluginorg.apache.maven.plugins:maven-compiler-plugin [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 109, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:3.3.0:describe (default-cli) hello-maven --- [INFO] org.apache.maven.plugins:maven-compiler-plugin:3.10.1Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of yourproject. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version: 3.10.1 Goal Prefix: compilerThis plugin has 3 goals:compiler:compileDescription: Compiles application sourcescompiler:helpDescription: Display help information on maven-compiler-plugin.Call mvn compiler:help -Ddetailtrue -Dgoalgoal-name to displayparameter details.compiler:testCompileDescription: Compiles application test sources.For more information, run mvn help:describe [...] -Ddetail[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.949 s [INFO] Finished at: 2023-10-15T17:20:3808:00 [INFO] Final Memory: 14M/309M [INFO] ------------------------------------------------------------------------进一步简化可以使用插件目标前缀替换坐标。例如 $ mvn help:describe -Dplugincompiler [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 109, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:3.3.0:describe (default-cli) hello-maven --- [INFO] org.apache.maven.plugins:maven-compiler-plugin:3.10.1Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of yourproject. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version: 3.10.1 Goal Prefix: compilerThis plugin has 3 goals:compiler:compileDescription: Compiles application sourcescompiler:helpDescription: Display help information on maven-compiler-plugin.Call mvn compiler:help -Ddetailtrue -Dgoalgoal-name to displayparameter details.compiler:testCompileDescription: Compiles application test sources.For more information, run mvn help:describe [...] -Ddetail[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.937 s [INFO] Finished at: 2023-10-15T17:21:3908:00 [INFO] Final Memory: 14M/309M [INFO] ------------------------------------------------------------------------如果想仅仅描述某个插件目标的信息可以加上 goal 参数 $ mvn help:describe -Dplugincompiler -Dgoalcompile [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 109, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:3.3.0:describe (default-cli) hello-maven --- [INFO] Mojo: compiler:compile compiler:compileDescription: Compiles application sourcesFor more information, run mvn help:describe [...] -Ddetail[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.822 s [INFO] Finished at: 2023-10-15T17:23:1908:00 [INFO] Final Memory: 14M/309M [INFO] ------------------------------------------------------------------------如果想让 maven-help-plugin 输出更详细的信息可以加上detail 参数 $ mvn help:describe -Dplugincompiler -Ddetail [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 109, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:3.3.0:describe (default-cli) hello-maven --- [INFO] org.apache.maven.plugins:maven-compiler-plugin:3.10.1Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of yourproject. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version: 3.10.1 Goal Prefix: compilerThis plugin has 3 goals:compiler:compileDescription: Compiles application sourcesImplementation: org.apache.maven.plugin.compiler.CompilerMojoLanguage: javaBound to phase: compileAvailable parameters:annotationProcessorPathsClasspath elements to supply as annotation processor path. If specified,the compiler will detect annotation processors only in those classpathelements. If omitted, the default classpath is used to detect annotationprocessors. The detection itself depends on the configuration ofannotationProcessors.Each classpath element is specified using their Maven coordinates(groupId, artifactId, version, classifier, type). Transitive dependenciesare added automatically. Example:configurationannotationProcessorPathspathgroupIdorg.sample/groupIdartifactIdsample-annotation-processor/artifactIdversion1.2.3/version/path!-- ... more ... --/annotationProcessorPaths/configurationannotationProcessorsNames of annotation processors to run. Only applies to JDK 1.6 If notset, the default annotation processors discovery process applies.......source (Default: 1.7)User property: maven.compiler.sourceThe -source argument for the Java compiler.NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since3.9.0 the default value has changed from 1.6 to 1.7staleMillis (Default: 0)User property: lastModGranularityMsSets the granularity in milliseconds of the last modification date fortesting whether a source needs recompilation.target (Default: 1.7)User property: maven.compiler.targetThe -target argument for the Java compiler.NOTE: Since 3.8.0 the default value has changed from 1.5 to 1.6. Since3.9.0 the default value has changed from 1.6 to 1.7useIncrementalCompilation (Default: true)User property: maven.compiler.useIncrementalCompilationto enable/disable incremental compilation feature.This leads to two different modes depending on the underlying compiler.The default javac compiler does the following:- true (default) in this mode the compiler plugin determines whether anyJAR files the current module depends on have changed in the currentbuild run; or any source file was added, removed or changed since thelast compilation. If this is the case, the compiler plugin recompilesall sources.- false (not recommended) this only compiles source files which are newerthan their corresponding class files, namely which have changed sincethe last compilation. This does not recompile other classes which usethe changed class, potentially leaving them with references to methodsthat no longer exist, leading to errors at runtime.verbose (Default: false)User property: maven.compiler.verboseSet to true to show messages about what the compiler is doing.compiler:helpDescription: Display help information on maven-compiler-plugin.Call mvn compiler:help -Ddetailtrue -Dgoalgoal-name to displayparameter details.Implementation: org.apache.maven.plugin.compiler.HelpMojoLanguage: javaAvailable parameters:detail (Default: false)User property: detailIf true, display all settable properties for each goal.goalUser property: goalThe name of the goal for which to show help. If unspecified, all goalswill be displayed.indentSize (Default: 2)User property: indentSizeThe number of spaces per indentation level, should be positive.lineLength (Default: 80)User property: lineLengthThe maximum length of a display line, should be positive.compiler:testCompileDescription: Compiles application test sources.Implementation: org.apache.maven.plugin.compiler.TestCompilerMojoLanguage: javaBound to phase: test-compileAvailable parameters:annotationProcessorPathsClasspath elements to supply as annotation processor path. If specified,the compiler will detect annotation processors only in those classpathelements. If omitted, the default classpath is used to detect annotationprocessors. The detection itself depends on the configuration ofannotationProcessors.Each classpath element is specified using their Maven coordinates(groupId, artifactId, version, classifier, type). Transitive dependenciesare added automatically. Example:configurationannotationProcessorPathspathgroupIdorg.sample/groupIdartifactIdsample-annotation-processor/artifactIdversion1.2.3/version/path!-- ... more ... --/annotationProcessorPaths/configurationannotationProcessorsNames of annotation processors to run. Only applies to JDK 1.6 If notset, the default annotation processors discovery process applies.compilerArgsSets the arguments to be passed to the compiler.Note that -J options are only passed through if fork is set to true.Example:compilerArgsarg-Xmaxerrs/argarg1000/argarg-Xlint/argarg-J-Duser.languageen_us/arg/compilerArgscompilerArgumentSets the unformatted single argument string to be passed to the compiler.To pass multiple arguments such as -Xmaxerrs 1000 (which are actually twoarguments) you have to use compilerArguments.This is because the list of valid arguments passed to a Java compilervaries based on the compiler version.Note that -J options are only passed through if fork is set to true.compilerArgumentsSets the arguments to be passed to the compiler (prepending a dash).This is because the list of valid arguments passed to a Java compilervaries based on the compiler version.Note that -J options are only passed through if fork is set to true.To pass -Xmaxerrs 1000 -Xlint -Xlint:-path -Averbosetrue you shouldinclude the following:compilerArgumentsXmaxerrs1000/XmaxerrsXlint/Xlint:-path/Averbosetrue/Averbose/compilerArgumentsDeprecated. use {link #compilerArgs} instead.compilerId (Default: javac)User property: maven.compiler.compilerIdThe compiler id of the compiler to use. See this guide for moreinformation.......testTargetUser property: maven.compiler.testTargetThe -target argument for the test Java compiler.useIncrementalCompilation (Default: true)User property: maven.compiler.useIncrementalCompilationto enable/disable incremental compilation feature.This leads to two different modes depending on the underlying compiler.The default javac compiler does the following:- true (default) in this mode the compiler plugin determines whether anyJAR files the current module depends on have changed in the currentbuild run; or any source file was added, removed or changed since thelast compilation. If this is the case, the compiler plugin recompilesall sources.- false (not recommended) this only compiles source files which are newerthan their corresponding class files, namely which have changed sincethe last compilation. This does not recompile other classes which usethe changed class, potentially leaving them with references to methodsthat no longer exist, leading to errors at runtime.verbose (Default: false)User property: maven.compiler.verboseSet to true to show messages about what the compiler is doing.[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.986 s [INFO] Finished at: 2023-10-15T17:25:4308:00 [INFO] Final Memory: 14M/309M [INFO] ------------------------------------------------------------------------读者可以在实际环境中使用 help:describe 描述一些常用插件的信息以得到更加直观的感受。 4.6 从命令行调用插件 如果在命令行运行 mvn -h 来显示mvn命令帮助就可以看到如下的信息 usage:mvn [options][goal(s)][phase(s)] Options: ...该信息告诉了我们 mvn 命令的基本用法options表示可用的选项mvn命令有20 多个选项这里暂不详述读 者可以根据说明来了解每个选项的作用。除了选项之外mvn命令后面可以添加一个或者多个goal 和 phase它 们分别是指插件目标和生命周期阶段。前面已经详细介绍了如何通过该参数控制 Maven 的生命周期。现在我们关 心的是另外一个参数:goal。 我们知道可以通过mvn命令激活生命周期阶段从而执行那些绑定在生命周期阶段上的插件目标。但 Maven 还 支持直接从命令行调用插件日标。Maven 支持这种方式是因为有些任务不适合绑定在生命周期上例如 maven-help-plugin:describe我们不需要在构建项目的时候去描述插件信息又如 maven-dependency-plugin:tree我们也不需要在构建项目的时候去显示依赖树。因此这些插件目标应该通过如 下方式使用 $ mvn help:describe -Dplugincompiler$ mvn dependency:tree不过这里还有一个疑问describe是 maven-help-plugin 的目标没错但冒号前面的help是什么呢?它既不是 groupld也不是artifactldMaven 是如何根据该信息找到对应版本插件的呢?同理为什么不是 maven-dependency-plugin:tree而是 dependency:tree? 解答该疑问之前可以先尝试一下如下的命令 $ mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:describe -Dplugincompiler [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-help-plugin:3.2.0:describe (default-cli) hello-maven --- [INFO] org.apache.maven.plugins:maven-compiler-plugin:3.10.1Name: Apache Maven Compiler Plugin Description: The Compiler Plugin is used to compile the sources of yourproject. Group Id: org.apache.maven.plugins Artifact Id: maven-compiler-plugin Version: 3.10.1 Goal Prefix: compilerThis plugin has 3 goals:compiler:compileDescription: Compiles application sourcescompiler:helpDescription: Display help information on maven-compiler-plugin.Call mvn compiler:help -Ddetailtrue -Dgoalgoal-name to displayparameter details.compiler:testCompileDescription: Compiles application test sources.For more information, run mvn help:describe [...] -Ddetail[INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.939 s [INFO] Finished at: 2023-10-15T17:37:3908:00 [INFO] Final Memory: 12M/309M [INFO] ------------------------------------------------------------------------$ mvn org.apache.maven.plugins:maven-dependency-plugin:3.2.0:tree [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for org.example:hello-maven:jar:1.0-SNAPSHOT [WARNING] build.plugins.plugin.(groupId:artifactId) must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-compiler-plugin line 69, column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building maven hello world 1.0-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] [INFO] --- maven-dependency-plugin:3.2.0:tree (default-cli) hello-maven --- [INFO] org.example:hello-maven:jar:1.0-SNAPSHOT [INFO] \- junit:junit:jar:4.13.2:test [INFO] \- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.871 s [INFO] Finished at: 2023-10-15T17:38:5308:00 [INFO] Final Memory: 15M/309M [INFO] ------------------------------------------------------------------------这两条命令就比较容易理解了插件的 groupld、artifactId、version以及goal都得以清晰描述。它们的效果与之 前的两条命令基本是一样的但显然前面的命令更简洁更容易记忆和使用。为了达到该目的Maven 引入了目 标前缀的概念help 是 maven-help-plugin 的目标前缀dependency 是 maven-dependency-plugin 的前缀 有了插件前缀Maven 就能找到对应的 artifactld。不过除了 artifactldMaven 还需要得到 groupId 和 version 才能精确定位到某个插件。 4.7 插件解析机制 为了方便用户使用和配置插件Maven 不需要用户提供完整的插件坐标信息就可以解析得到正确的插件 Maven 的这一特性是一把双刃剑虽然它简化了插件的使用和配置可一旦插件的行为出现异常用户就很难快 速定位到出问题的插件构件。例如 mvn help:system 这样一条命令它到底执行了什么插件?该插件的 groupId、 artifactld 和version 分别是什么?这个构件是从哪里来的?本节就详细介绍 Maven的运行机制以让读者不仅知其 然更知其所以然。 4.7.1 插件仓库 与依赖构件一样插件构件同样基于坐标存储在 Maven 仓库中。在需要的时候Maven 会从本地仓库寻找插 件如果不存在则从远程仓库查找。找到插件之后再下载到本地仓库使用。 值得一提的是Maven会区别对待依赖的远程仓库与插件的远程仓库前面介绍了如何配置远程仓库但那种配 置只对一般依赖有效果。当 Maven 需要的依赖在本地仓库不存在时它会去所配置的远程仓库查找可是当 Maven 需要的插件在本地仓库不存在时它就不会去这些远程仓库查找。 不同于repositories 及其 repository 子元素插件的远程仓库使用 pluginRepositories 和 pluginRepository 配 置。例如Maven 内置了如下的插件远程仓库配置。 pluginRepositoriespluginRepositoryidcentral/idnameMaven Plugin Repository/nameurlhttps://repo1.maven.org/maven2//urllayoutdefault/layoutsnapshotsenabledfalse/enabled/snapshotsreleasesupdatePolicynever/updatePolicy/releases/pluginRepository /pluginRepositories除了 pluginRepositories 和 pluginRepository 标签不同之外其余所有子元素表达的含义与前面所介绍的依赖远 程仓库配置完全一样。我们甚至看到这个默认插件仓库的地址就是中央仓库它关闭了对SNAPSHOT的支持 以防止引入SNAPSHOT版本的插件而导致不稳定的构建。 一般来说中央仓库所包含的插件完全能够满足我们的需要因此也不需要配置其他的插件仓库。只有在很少的情 况下项目使用的插件无法在中央仓库找到或者自己编写了插件这个时候可以参考上述的配置在 POM 或者 settings.xml 中加人其他的插件仓库配置。 4.7.2 插件的默认 groupld 在POM中配置插件的时候如果该插件是 Maven 的官方插件(即如果其 groupld 为 org.apache.maven.plugins)就可以省略 groupld配置。 pluginartifactIdmaven-compiler-plugin/artifactIdversion3.10.1/versionconfigurationsource1.8/sourcetarget1.8/target/configuration /plugin上述配置中省略了 maven-compiler-plugin 的 groupldMaven 在解析该插件的时候会自动用默认 groupld org.apache. maven. plugins 补齐。 笔者不推荐使用 Maven 的这一机制虽然这么做可以省略一些配置但这样的配置会让团队中不熟悉Maven的成 员感到费解况且能省略的配置也就仅仅一行而已。 4.7.3 解析插件版本 同样是为了简化插件的配置和使用在用户没有提供插件版本的情况下Maven 会自动解析插件版本。 首先Maven在超级POM中为所有核心插件设定了版本超级POM是所有Maven项目的父 POM所有项目都继 承这个超级 POM 的配置因此即使用户不加任何配置Maven 使用核心插件的时候它们的版本就已经确定 了。这些插件包括 maven-clean-plugin、maven-compiler-plugin、maven-surefire-plugin等。 如果用户使用某个插件时没有设定版本而这个插件又不属于核心插件的范畴Maven 就会去检查所有仓库中可 用的版本然后做出选择。前面介绍的仓库元数据 groupId/artifactld/maven-metadata.xml。 以 maven-compiler-plugin 为例它在中央仓库的仓库元数据为 http://repo1.maven.org/maven2/org/apache/maven/plugins/maven-compiler-plugin/maven-metadata.xml 其内容见代码 metadata groupIdorg.apache.maven.plugins/groupId artifactIdmaven-compiler-plugin/artifactId versioning latest3.11.0/latest release3.11.0/release versions version2.0-beta-1/version version2.0/version version2.0.1/version version2.0.2/version version2.1/version version2.2/version version2.3/version version2.3.1/version version2.3.2/version version2.4/version version2.5/version version2.5.1/version version3.0/version version3.1/version version3.2/version version3.3/version version3.5/version version3.5.1/version version3.6.0/version version3.6.1/version version3.6.2/version version3.7.0/version version3.8.0/version version3.8.1/version version3.9.0/version version3.10.0/version version3.10.1/version version3.11.0/version /versions lastUpdated20230227075825/lastUpdated /versioning /metadataMaven 遍历本地仓库和所有远程插件仓库将该路径下的仓库元数据归并后就能计算出 latest 和 release 的 值。latest 表示所有仓库中该构件的最新版本而release 表示最新的非快照版本。在Maven 2中插件的版本会 被解析至latest。也就是说当用户使用某个非核心插件且没有声明版本的时候Maven 会将版本解析为所有可用 仓库中的最新版本而这个版本也可能是快照版。 当插件的版本为快照版本时就会出现潜在的问题。Maven 会基于更新策略检查并使用快照的更新。某个插件 可能昨天还用得好好的今天就出错了其原因就是这个快照版本的插件发生了变化。为了防止这类问题 Maven 3 调整了解析机制当插件没有声明版本的时候不再解析至latest而是使用release。这样就可以避免 由于快照频繁更新而导致的插件行为不稳定。 依赖 Maven 解析插件版本其实是不推荐的做法即使 Maven 3 将版本解析到最新的非快照版也还是会有潜在 的不稳定性。例如可能某个插件发布了一个新的版本而这个版本的行为与之前的版本发生了变化这种变化就 可能导致项目构建失败。因此使用插件的时候应该一直显式地设定版本这也解释了 Maven 为什么要在超级 POM 中为核心插件设定版本。 4.7.4 解析插件前缀 前面讲到 mvn 命令行支持使用插件前缀来简化插件的调用现在解释 Maven 如何根据插件前缀解析得到插件的 坐标。 插件前缀与 groupld:artifactld 是一一对应的这种匹配关系存储在仓库数据中。与之前提到的 groupId/artifactId/maven-metadata.xml 不同这里的仓库元数据为 groupld/maven-metadata.xml那么这里的 groupld 是什么呢? 前面提到主要的插件都位于 https://repo1.maven.org/maven2/org/apache/maven/plugins/ 和 http://repository.codehaus.org/org/codehaus/mojo/ 相应地Maven 在解析插件仓库元数据的时候会默认使用 org.apache.maven.plugins 和 org.codehaus.mojo 两个 groupld。也可以通过配置 settings.xml 让 Maven 检查其他groupld 上的插件仓库元数据 settingspluginGroupspluginGroupcom.your.plugins/pluginGroup/pluginGroups /settings基于该配置Maven 就不仅仅会检查 org/apache/maven/plugins/maven-metadata.xml 和 org/codehaus/mojo/maven-metadata.xml还会检查 com/your/plugins/maven-metadata.xml。 下面看一下插件仓库元数据的内容见代码。 metadatapluginspluginnameMaven Clean Plugin/nameprefixclean/prefixartifactIdmaven-clean-plugin/artifactId/pluginpluginnameMaven Compiler Plugin/nameprefixcompiler/prefixartifactIdmaven-compiler-plugin/artifactId/pluginpluginnameMaven Dependency Plugin/nameprefixdependency/prefixartifactIdmaven-dependency-plugin/artifactId/plugin/plugins /metadata上述内容是从中央仓库的 org.apache.maven.plugins groupld 下插件仓库元数据中截取的一些片段从这段数据 中就能看到 maven-clean-plugin 的前缀为 cleanmaven-compiler-plugin 的前缀为 compiler maven-dependency-plugin 的前缀为 dependency。 当 Maven 解析到 dependency:tree 这样的命令后它首先基于默认的 groupld 归并所有插件仓库的元数据 org/apache/maven/plugins/maven-metadata.xml其次检查归并后的元数据找到对应的 artifactld 为 maven-dependency-plugin然后结合当前元数据的 groupld org.apache. maven.plugins最后使用上节描述 的方法解析得到 version这时就得到了完整的插件坐标。如果 org/apache/maven/plugins/maven-metadata.xml 没有记录该插件前缀则接着检查其他 groupld 下的元数 据如 org/codehaus/mojo/maven-metadata.xml以及用户自定义的插件组。如果所有元数据中都不包含该 前缀则报错。
http://www.hkea.cn/news/14464521/

相关文章:

  • asp.net网站开发框架自己做的导航网站
  • 主机服务器网站 怎么做大理州建设局网站
  • 亚马逊海外网站做网站用c 还是php
  • 普通网站设计网站建设结课策划书
  • 河南网站搭建怎么做wordpress主题
  • 有哪些做农产品的网站有哪些电子加工东莞网站建设
  • 国内响应式布局网站安阳网络教研平台首页
  • 做钓鱼网站会被抓吗外贸在哪个网站做
  • 建购物网站 教程wordpress添加热门文章
  • 做网站维护合同郴州人为什么不像湖南人
  • wordpress 一些数据表不可用深圳seo顾问
  • 互动型网站成功例子东莞建设网东莞市住房和城乡建设
  • 网站建设先学什么宁夏做网站好的公司
  • 沧州网站营销推广南京快速建站公司
  • 图床网站怎么做自学网站开发哪个网站好
  • 做音乐网站的栏目做网站界面用的软件
  • jsp mysql 网站开发吉林网站建设
  • scala网站开发郑州app开发公司
  • 用电脑做网站的历史在哪里找百度seo手机
  • 网站建设要钱么国内知名的网站建设公司
  • 国外手机网站国内做网站建设好的
  • 做餐饮类网站用哪个程序网络推广有哪些方法
  • 乐清手机网站广告发光字制作培训班
  • 做营销型网站公司电影网站内页
  • 网站开发与系统开发室内设计师工作室
  • 中国能建设计公司网站专门代写平台
  • 常用的网站开发语言平面设计和网页设计哪个好
  • 施工程找工程做哪个网站好深圳网站制作建设服务公司
  • 外贸网站模板 外贸网站制作公共资源交易中心有实权吗
  • 做旅游网站用什么颜色永兴网站开发