【问题标题】:Automatically executing dependency:build-classpath in maven自动执行依赖:maven中的build-classpath
【发布时间】:2014-02-24 16:23:11
【问题描述】:

我确实在尝试这样做:

http://maven.apache.org/plugins/maven-dependency-plugin/usage.html#The_dependency:build-classpath_mojo

令人惊奇的是,在找到了一个明确的例子来说明我想要 Maven 做什么之后.. 我仍然无法让它工作。

从命令行,我可以运行... mvn -Dmdep.outputFile=classpath.txt 依赖:build-classpath ...确实会生成一个名为 classpath.txt 的文件,其中包含我想要的信息。

我希望能够发出类似“mvn compile”的命令,并让这个 classpath.txt 文件的生成成为该过程的一部分。上面链接中提供的示例将其与 generate-sources 相关联,据我了解,这应该足够了。

当使用下面的 pom sn-p 执行“mvn compile”之类的命令时,似乎没有执行任何关于 build-classpath 目标的操作。

<build>
<pluginManagement>
  <plugins>      

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>

        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>

     </plugin>

     <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-dependency-plugin</artifactId>
      <version>2.8</version>
      <executions>
       <execution>
        <id>build-classpath</id>
        <phase>generate-sources</phase>
        <goals>
          <goal>build-classpath</goal>
        </goals>
         <configuration>
            <outputFile>myfile.txt</outputFile>
            <mdep.outputFile>myFile1.txt</mdep.outputFile>
            <ihavenoidea>whatgoeshere</ihavenoidea>
         </configuration>
       </execution>
     </executions>
    </plugin>
  </plugins>
 </pluginManagement>
</build>

这是我最终的结果:

$ mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building someproj 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ someproj ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ someproj ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.600s
[INFO] Finished at: Fri Jan 31 14:05:29 CST 2014
[INFO] Final Memory: 9M/156M
[INFO] ------------------------------------------------------------------------

$ ls 
bin     html        log     pom.xml     resources   sql     src     target      test-output wwwroot

【问题讨论】:

  • StackExchange 很棒。我知道这一点,因为在发布我的问题后不久,我就自己解决了。 :|删除“PluginManagement”元素解决了这个问题,但我不完全确定为什么。有什么帮助吗?
  • 欢迎来到 StackOverflow!请为您的问题添加答案并接受它作为答案。这样,搜索您的问题的人也会找到您的解决方案。

标签: java maven plugins pom.xml


【解决方案1】:

您的插件定义在&lt;pluginManagement&gt; 中,这意味着当您在以该 pom 为父(或该 pom 本身)的 pom 中声明该插件的“真实”执行时,它将使用该配置。

当一个通用配置必须通过同一个全局项目中的多个模块应用于多个执行时,通常使用&lt;pluginManagement&gt; 是一个好主意。

在这里,我个人会将编译器插件保留在 &lt;pluginManagement&gt; 中,因为您可能总是希望该插件配置为这样,但我会将依赖插件移动到 &lt;plugins&gt; 部分内(在 &lt;pluginManagement&gt; 之外部分,是的,这可能会令人困惑......)

【讨论】:

  • 就是这样。 pluginManagement 使 maven 将我的添加更像是一个模板,显然是针对子 pom 的。这里还有另一个类似的帖子:stackoverflow.com/questions/10483180/… 谢谢你的帮助。
【解决方案2】:

您可能会将&lt;pluginManagement&gt; 视为一种模板。它通常在父 POM 中用于定义通用配置。只有&lt;build&gt;&lt;plugins&gt; 中的插件包含在构建中。

也就是说,Maven 确实根据包装类型做了一些“魔术”。我回答了一个类似的问题here

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多