2

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.

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273

1 Answers1

4

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
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • It's the 3rd option :) In fact my line is a `` with specific information (such as an `output` information), so these plugins do not seem to have the adequate configuration... – Romain Linsolas Feb 10 '11 at 16:28
  • @Sean Thanks, I know quite well Maven in fact, but this is not what I want. If you want to understand the complete problem, see my other question here: http://stackoverflow.com/questions/4956245/how-to-manage-linked-resources-in-eclipse-for-correct-tomcat-deployment – Romain Linsolas Feb 10 '11 at 21:09
  • @Sean, indeed, this solution is not really nice (thanks anyway). In fact, I already solved my issue in creating a little Maven plugin that is run just after the `mvn eclipse:eclipse`, which edits the `.classpath` file. But I wanted to find a way to only use the `maven-eclipse-plugin`. – Romain Linsolas Feb 10 '11 at 22:05
  • @romaintaz yup, the only way to do it in one step is to let your plugin extend the `eclipse:eclipse` plugin goal. – Sean Patrick Floyd Feb 10 '11 at 22:09
  • @romaintaz thanks. and my answer is essentially the same as what you did, just that I proposed an inline groovy solution whereas you used a custom plugin to do the same thing – Sean Patrick Floyd Feb 11 '11 at 12:16