Lance
Lance

Reputation: 23

Automatically executing dependency:build-classpath in maven

I am literally trying to do exactly this:

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

What's amazing is that after finding an explicit example of exactly what I want Maven to do.. I still can't get it to work.

From the command line, I can run ... mvn -Dmdep.outputFile=classpath.txt dependency:build-classpath ... which does indeed produce a file called classpath.txt with the information I'd like.

I would like to be able to issue a command like "mvn compile" and have the production of this classpath.txt file be a part of that process. The example provided at the link above associates it with generate-sources, which to my understanding should suffice.

When executing a command like "mvn compile" with this pom snippet below, nothing regarding the build-classpath goal seems to execute.

<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>

And here is what I end up with:

$ 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

Upvotes: 2

Views: 4169

Answers (2)

user944849
user944849

Reputation: 14951

You may think of <pluginManagement> as a kind of template. It's often used in parent POMs to define a common configuration. Only plugins in <build><plugins> are included in the build.

That said, Maven does do some "magic" depending on the packaging type. I answered a similar question here.

Upvotes: 0

Tome
Tome

Reputation: 3364

Your plugin definition is inside <pluginManagement>, which means that when you will declare a "real" execution of that plugin inside a pom that has this pom as parent (or this pom itself), it will use that configuration.

This is generaly a good idea to use <pluginManagement> when a common configuration has to be applied on multiple execution, through multiple modules in the same global project.

Here, I would personally keep the compiler plugin inside <pluginManagement>, as you probably always want that plugin to be configured like this, but I woul move the dependency-plugin inside the <plugins> section (outside the <pluginManagement> section, well yes, this can be confusive...)

Upvotes: 1

Related Questions