如何在 Maven 3 中指定插件的顺序(绑定到同一阶段)?
我有一个使用 Maven 3 的多模块 Scala 项目。 我想确保 Scala 编译器在编译和测试编译阶段都在 Java 编译器之前运行。
我已经尝试过,在我的父 POM 和模块 POM 中,在构建部分列出插件,以便 Scala 编译器插件位于第一个,但 Maven 仍然坚持首先运行 Java 编译,这在我混合了某些情况下会失败来源。
我知道我可以通过将 Scala 编译绑定到进程资源而不是编译来解决这个问题,但我真的更想知道如何告诉 Maven 订购插件(或者这是否可能)。
这是我的父 POM 中定义这两个插件的部分:
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
子模块有自己的
I have a multi-module Scala project using Maven 3.
I want to make sure that the Scala compiler runs before the Java compiler, both for compile and test-compile phases.
I have tried, in both my parent POM and module POM, listing the plugins in the build section so that the Scala compiler plugin is first, and yet Maven still insists on running the Java compilation first, which fails in some cases where I have mixed source.
I know I can solve this by binding the Scala compilation to process-resources instead of compile, but I'd really prefer to know how I can tell Maven to order the plugins (or whether this is possible).
Here is the part of my parent POM that defines these two plugins:
<plugin>
<groupId>org.scala-tools</groupId>
<artifactId>maven-scala-plugin</artifactId>
<version>2.15.2</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
</configuration>
<executions>
<execution>
<id>compile</id>
<goals>
<goal>compile</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<phase>test-compile</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
The child module has its own <plugins> section, but doesn't re-define either of these plugins (just the exec-maven-plugin).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Maven 开发团队已确认目前,除非编写您自己的自定义生命周期并使用自定义包类型,否则不可能完全控制一个阶段内插件执行的顺序。
我已经在 JIRA 中创建了增强请求,以便他们考虑修复此问题。如果这对您来说是一个问题,请投票。
目前看来,针对此特定问题(混合 Java/Scala 编译)的“最佳实践”解决方法是将 scala:compile 绑定到 process-resources,将 scala:testCompile 绑定到 process-test-resources,如 http://scala-tools.org/mvnsites/maven-scala-plugin/example_java.html
The Maven development team has confirmed that currently it is not possible to completely control the order of plugin execution within a phase, except by writing your own custom Lifecycle and using a custom Package Type.
I've created an enhancement request in JIRA for them to consider fixing this. Please vote for it if this is an issue for you.
It seems for the moment that the 'best practice' workaround for this specific problem (Mixed Java/Scala compilation) is to bind the scala:compile to process-resources and scala:testCompile to process-test-resources, as specified at http://scala-tools.org/mvnsites/maven-scala-plugin/example_java.html
你可以一起运行 2 个单独的目标,maven 将按顺序执行目标:
you can run the 2 separate goals together and maven will execute the goals in sequence:
要编译混合 Java/Scala 项目,实际上比先编译一个再编译另一个项目要复杂一些。除此之外,Scala 编译器能够解析 Java 文件,以便匹配您在 Scala 中使用的任何 Java 类的签名。编译器基本上可以在这里为您做正确的事情,但您必须首先告诉它这样做。
最好的选择是使用 SBT 而不是 Maven,因为除了获得正确的编译顺序之外,它还提供许多好处。
如果无法放弃 Maven,请按照此处的 maven-scala-plugin 配置说明进行操作:
To compile a mixed Java/Scala project, it's actually a bit more complicated than just compiling one then the other. Amongst other things, the Scala compiler is able to parse Java files in order to match up signatures for any Java classes that you're using from Scala. The compiler can essentially do the right thing for you here, but you must first tell it to do so.
Your best bet is to use SBT instead of Maven, as it'll offer many benefits beyond just getting the compilation order right.
If moving away from Maven is not an option, then follow the configuration instructions for the maven-scala-plugin here: http://scala-tools.org/mvnsites/maven-scala-plugin/example_java.html
maven编译器插件的
id
是default-compile
吗?如果不行的话可以设置一下试试看是否有效?如果没有,您可以使用相关的 pom 片段和 Maven 运行中的片段更新问题。
Is the
id
for the maven compiler plugin todefault-compile
? If not, can you set it and try if it works?If not, you could update the question with relevant pom snippet and the snippet from the maven run.