【问题标题】:Is there a way to add a custom line in .classpath using mvn eclipse:eclipse?有没有办法使用 mvn eclipse:eclipse 在 .classpath 中添加自定义行?
【发布时间】:2011-06-24 23:14:36
【问题描述】:

我正在使用mvn eclipse:eclipse 命令生成我的.project.classpath

但是,由于某些原因,我想在.classpath 文件中添加一行。我的pom.xml 中是否有可以用来实现此目的的配置?

注意不能使用<additionalConfig>,因为这会删除.classpath的内容。

我正在使用 maven 3.0.2 和 maven-eclipse-plugin 2.8。

【问题讨论】:

    标签: maven maven-eclipse-plugin


    【解决方案1】:

    这取决于那条线是什么。

    1. 如果是源文件夹,请使用 buildhelper-maven-pluginadd a source folder 在一个 生命周期阶段之前 generate-sources,这将 会被自动捡起 eclipse 插件。
    2. 如果它是一个类路径容器,你 可以使用classpathContainers 参数
    3. 如果您想更改输出文件夹(从 target/classestarget/test-classes 更改为其他文件夹),请在 maven 构建配置中更改它们:

      <build>
          <!-- replace "target" -->
          <directory>somedir</directory>
          <!-- replace "target/classes" -->
          <outputDirectory>anotherdir</outputDirectory>
          <!-- replace "target/test-classes" -->
          <testOutputDirectory>yetanotherdir</testOutputDirectory>
      </build>
      

      您可以单独配置这三个,这些更改将由 eclipse 插件获取,但将outputDirectorytestOutputDirectory 放入directory 被认为是一种好习惯(通常通过引用${project.build.directory}) ,否则你会破坏像mvn clean 这样的标准功能(它会清除${project.build.directory}):

      <build>
          <directory>bin</directory>
          <outputDirectory>${project.build.directory}/main-classes
          </outputDirectory>
          <!-- this config will replace "target" with "bin",
               compile src/main/java to "bin/main-classes"
               and compile src/test/java to "bin/test-classes"
               (because the default config for <testOutputDirectory> is
               ${project.build.directory}/test-classes )
          -->
      </build>
      

    参考:


    更新:在您的情况下,我想唯一可能的解决方案是以编程方式编辑 .classpath 文件。我可能会做的是这样的:

    1. 定义一个名为 eclipse 的&lt;profile&gt;(或其他)
    2. 定义gmaven plugin的执行(使用this version
    3. 编写一个简短的 groovy 脚本(内联在 pom 中或外部),检查您的类路径容器的 .classpath 文件并在缺少时添加它(将执行绑定到生命周期阶段,例如 generate-resources
    4. profile activation 设置为&lt;file&gt;&lt;exists&gt;${project.basedir}/.classpath&lt;/exists&gt;&lt;/file&gt;(因为您只希望它在eclipse 项目中处于活动状态)

    此解决方案的问题:eclipse:eclipse 是一个目标,而不是一个阶段,因此无法自动执行此操作,因此您必须执行以下操作:

    mvn eclipse:eclipse    # two separate executions
    mvn generate-resources # here, profile will be active
    

    或者这也可以:

    mvn -Peclipse eclipse:eclipse generate-resources
    

    【讨论】:

    • 这是第三个选项 :) 实际上我的行是带有特定信息的&lt;classpathentry/&gt;(例如output 信息),所以这些插件似乎没有足够的配置......
    • @Sean 谢谢,事实上我很了解 Maven,但这不是我想要的。如果您想了解完整的问题,请在此处查看我的另一个问题:stackoverflow.com/questions/4956245/…
    • @Sean,确实,这个解决方案不是很好(无论如何谢谢)。事实上,我已经解决了创建一个小 Maven 插件的问题,该插件在 mvn eclipse:eclipse 之后运行,它编辑 .classpath 文件。但我想找到一种只使用maven-eclipse-plugin的方法。
    • @romaintaz 是的,一步完成的唯一方法是让您的插件扩展 eclipse:eclipse 插件目标。
    • @romaintaz 谢谢。我的回答与您所做的基本相同,只是我提出了一个内联的 groovy 解决方案,而您使用自定义插件来做同样的事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多