有没有办法使用 mvn eclipse:eclipse 在 .classpath 中添加自定义行?

发布于 2024-10-16 19:53:56 字数 342 浏览 2 评论 0原文

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

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

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

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

I am using mvn eclipse:eclipse command to generate my .project and .classpath.

However, for some reasons, I want to add one line in the .classpath file. Is there a configuration in my pom.xml that I can use to achieve that?

Note that <additionalConfig> cannot be used, as this will erase the content of the .classpath.

I am using maven 3.0.2 and maven-eclipse-plugin 2.8.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不羁少年 2024-10-23 19:53:56

这取决于那条线是什么。

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

    <预置><代码><构建>

    <目录>somedir

    anotherdir

    yetanotherdir

    您可以独立配置这三者中的每一个,并且 Eclipse 插件将拾取所做的更改,但将 outputDirectorytestOutputDirectory 放入< code>directory (通常通过引用 ${project.build.directory}),否则您会破坏 mvn clean 等标准功能(它会清理 $ {project.build.directory}):

    <预置><代码><构建>
    <目录>bin
    ${project.build.directory}/main-classes



参考:


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

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

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

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

或者也许这也可以工作:

mvn -Peclipse eclipse:eclipse generate-resources

It depends on what that line is.

  1. If it's a source folder, use the
    buildhelper-maven-plugin to
    add a source folder in a
    lifecycle phase before
    generate-sources, this will
    automatically be picked up by the
    eclipse plugin.
  2. If it's a classpath container, you
    can use the classpathContainers
    parameter
  3. If you want to change the output folders (from target/classes and target/test-classes to something else), change them in the maven build configuration:

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

    You can configure each of those three independently, and the changes will be picked up by the eclipse plugin, but it's considered good practice to put outputDirectory and testOutputDirectory inside directory (usually by referencing ${project.build.directory}), otherwise you break standard functionality like mvn clean (it cleans ${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>
    

Reference:


Update: in your case I guess the only possible solution is to programmatically edit the .classpath file. What I would probably do is something like this:

  1. Define a <profile> named eclipse (or whatever)
  2. Define an execution of the gmaven plugin (use this version)
  3. Write a short groovy script (inline in the pom or external) that checks the .classpath file for your classpath container and adds it if missing (bind execution to a lifecycle phase, e.g. generate-resources)
  4. set the profile activation to <file><exists>${project.basedir}/.classpath</exists></file> (because you only want it to be active in an eclipse project)

The problem with this solution: eclipse:eclipse is a goal, not a phase, so it's not possible to execute this automatically, so you'll have to do something like this:

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

or perhaps this will also work:

mvn -Peclipse eclipse:eclipse generate-resources
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文