spauny
spauny

Reputation: 5096

Maven - use assembly file and configuration from the parent project

I have a parent project that defines the entire configuration for the assembly plugin including the assembly.xml file (creates a tar.gz). Now I want to have multiple children that can use the parent's project configuration and the parent's assembly file.

Now I tried different scenarios and the configuration works fine but I get the error that assembly file is not to be found locally. What I want to achieve is not having to configure and add the same file over and over again in all the projects. Is there a way to achieve this goal?

Upvotes: 1

Views: 4255

Answers (3)

user944849
user944849

Reputation: 14951

I use the maven-remote-resources-plugin to share assembly descriptors. First, create a separate project that holds the assembly descriptors you want to share.

project-assemblies
  src
    main
      resources
        <your assembly descriptors, in any package structure you like>
  pom.xml

The POM's build section (assume this is project com.mycompany.project:project-assemblies):

<build>
  <plugins>
      <plugin>
          <artifactId>maven-remote-resources-plugin</artifactId>
          <executions>
              <execution>
                  <goals>
                      <goal>bundle</goal>
                  </goals>
              </execution>
          </executions>
          <configuration>
              <includes>
                  <include>**/*.xml</include>
              </includes>
          </configuration>
      </plugin>
   </plugins>
</build>

To reference this from another project you add this to the project's POM:

<properties>
  <shared.assemblies.dir>${project.build.directory}/assemblies</shared.assemblies.dir>
  <project-assemblies.version><yourAssemblyProjectVersion</project-assemblies.version>
</properties>

<!-- copy the assembly descriptors into this project -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-remote-resources-plugin</artifactId>
  <executions>
    <execution>
      <id>assemblies</id>
      <phase>initialize</phase>
      <goals>
        <goal>process</goal>
      </goals>
      <configuration>
        <resourceBundles>
          <resourceBundle>com.mycompany.project:project-assemblies:${project-assembly.version}</resourceBundle>
        </resourceBundles>
        <attached>false</attached>
        <outputDirectory>${shared.assemblies.dir}</outputDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

Setting attached to false means the contents of the bundle won't be included in the final project build.

<!-- Apply the retrieved assembly descriptor to this project -->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <id>run-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
      <configuration>
        <descriptors>
          <descriptor>${shared.assemblies.dir}/<pathToAssemblyDescriptor></descriptor>
        </descriptors>
        <!-- other config here -->
      </configuration>
    </execution>
  </executions>
</plugin>

Works like a charm.

Upvotes: 1

Laurence Geng
Laurence Geng

Reputation: 434

Well, create a dedicated sub-module to config assemble plugin rather than config assembly plugin in parent pom.xml

as for sample, please refer to: http://maven.apache.org/plugins/maven-assembly-plugin/examples/multimodule/module-binary-inclusion-simple.html

In this sample, it creates an sub-module called: distribution

This is an good example for you! Good luck!

Upvotes: 0

Laurence Geng
Laurence Geng

Reputation: 434

You should disable assembly in all sub-module's pom.xml:

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
    <skipAssembly>true</skipAssembly>
</configuration>
</plugin>

And more often, it's recommend to create a dedicated sub-module to config assemble plugin rather than config assembly plugin in parent pom.xml

Upvotes: 1

Related Questions