Search code examples
javamavenxsdwsdlmaven-dependency-plugin

How to choose which maven executions are run?


THE SETUP

I have a complex project that I'm trying to build using Maven. What follows is a simplified example of that project. The directory structure is as follows:

  • main - The project has more than one 'ear', each of which contains more than one 'war' plus other supporting jars. Yet they share a common set of WSDL/XSD, as well as Java code.
    • pom.xml - This is the main pom.xml file, I'll describe it in more detail below.
    • common-code-one - the two 'ear' package have a body of code in common, it's just java code, no wsdl, no xsd, so no problem.
    • wsdl - single package holding all the XSD and WSDL files, it has a pom.xml that simply packs it all up inside a jar file, for later use in sub-directories.
    • ear-package-one
      • common-code-two - Some of the XSD from 'wsdl' is unpacked here, and turned into code via jaxb2. As part of that, an episode file is generated.
      • war-package-one - More of the XSD and WSDL files are unpacked here, and turned into code via jaxws. To avoid having duplicate code, I need the episode file from 'common-code-two', and I need the XSD/WSDL files from 'wsdl'.
      • war-package-two - Different XSD/WSDL, but I still need the episode file from common-code-two, as well as the XSD/WSDL from 'wsdl'.
      • ejb-package-one - Don't need anything from 'wsdl', but may need some of the code from 'common-code-two', without needing the episode file.
      • ...
    • ear-package-two
      • common-code-two - Same problem, different XSD.
      • ejb-package-two - Same problem, different code.
      • war-package-three - Same problem, different XSD/WSDL.
      • war-package-four - Same problem, different XSD/WSDL.

Now, the main 'pom.xml' file. Following Maven's DRY principle, I've put both executions of the maven-dependency-plugin into the main pom.xml. The problem is that in some of the sub-packages above, I only want the 'unpack-schema' execution to run, in others I want both the 'unpack-schema' and 'unpack-common-binding' to run. Of course, for the ones that don't need either, I just don't add the dependency plugin to the build list.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>

    <execution>
      <id>unpack-schema</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
    <artifactItems>
      <artifactItem>
        <groupId>com.foo.bar</groupId>
        <artifactId>wsdl</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>jar</type>
        <overWrite>true</overWrite>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <includes>${schema.include.list}</includes>
      </artifactItem>
    </artifactItems>
    <overWriteReleases>true</overWriteReleases>
    <overWriteSnapshots>true</overWriteSnapshots>
      </configuration>
    </execution>

    <execution>
      <id>unpack-common-binding</id>
      <phase>generate-sources</phase>
      <goals>
    <goal>unpack</goal>
      </goals>
      <configuration>
    <artifactItems>
      <artifactItem>
        <groupId>com.foo.bar</groupId>
        <artifactId>${common.code.jar}</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <type>jar</type>
        <overWrite>true</overWrite>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <includes>**/commonCode.xml</includes>
      </artifactItem>
    </artifactItems>
    <overWriteReleases>true</overWriteReleases>
    <overWriteSnapshots>true</overWriteSnapshots>
      </configuration>
    </execution>

  </executions>
</plugin>

THE QUESTION

What do I put in the sub-projects' pom.xml files so that only the executions that are desired happen?

If I simply put in

  <build>
    <plugins>
...
      <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
      </plugin>
...
    </plugins>
  </build>

then both executions happen. In the 'war' packages, that's ok, that's what I want to have happen, but in the 'common-code' packages, I only want the 'unpack-schema' execution to happen.

I've seen this question but it was never really answered, and the commented solution (run multiple builds) just isn't suitable for my environment. This is going to be going through continuous integration/build, so it has to compile straight up. No scripts, no multiple build passes.


Solution

  • It's easy, most plugins have an <skip/> configuration item, dependency plugin is no exception!

    so, you can overwrite the pom of common-code with <skip/>to disable a certain execution! for example:

     <execution>
      <id>unpack-schema</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <skip>true</skip>
      </configuration>
    </execution>
    

    BTW: a tip for you: for multi-module projects, a plugin configured at parent pom.xml will run on all sub-modules, unless you disable it on sub-module's pom.xml with !

    Good Luck!