1

I have problem with assembly plugin. I have to use one assembly definition which create JAR file for more modules. So project structure looks like this

module/
----module1/
-------- pom.xml
----module2/
-------- pom.xml
----moduleX/
-------- pom.xml
assembly_jar.xml
pom.xml (this is parent)

Parent pom file looks like following:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.my.project</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                  <execution>
                    <id>make-jar</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <attach>false</attach>
                        <descriptors>
                            <descriptor>${source-systems.basedir}/assembly_jar.xml</descriptor>
                        </descriptors>
                        <finalName>${project.artifactId}-${project.version}</finalName>
                        <appendAssemblyId>false</appendAssemblyId>
                    </configuration>
                </execution>
            </plugin>
        </plugins>
    </build>
</project>

assembly_jar.xml looks like following:

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
  <id>jar-with-some-dependencies</id>
  <formats>
    <format>jar</format>
  </formats>
  <includeBaseDirectory>false</includeBaseDirectory>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/</outputDirectory>
      <useProjectArtifact>true</useProjectArtifact>
      <scope>runtime</scope>
      <unpack>true</unpack>
    </dependencySet>
  </dependencySets>
</assembly>

pom file of module 1 looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent> 
    <groupId>com.my.project</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <packaging>pom</packaging>
  <groupId>com.my.project</groupId>
  <artifactId>module1</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>oracle_rds</name>

  <dependencies>
    <dependency>
      <groupId>com.oracle</groupId>
      <artifactId>ojdbc6</artifactId>
      <version>11.2.0.4</version>
    </dependency>
  </dependencies>

</project>

When I run mvn clean install for module1 everything works fine and jar file is created. But then I have module2 which does not need any particular dependencies. So pom file looks like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <parent> 
    <groupId>com.my.project</groupId>
    <artifactId>parent-module</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <packaging>pom</packaging>
  <groupId>com.my.project</groupId>
  <artifactId>module2</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <name>oracle_rds</name>

</project>

When I run mvn clean install I have error Error creating assembly archive jar-with-some-dependencies: archive cannot be empty It is because there is no dependency to pack into jar file.

Is there any way how to ignore this error and do not create jar file if there are not dependencies in module?

I do not want to move assembly definition into module because of big duplicity (I will have more then 100 modules and mostly of them will have some special dependencies) Thanks for help.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Juraj
  • 738
  • 2
  • 9
  • 26
  • I think you should take a deeper look into the docs https://maven.apache.org/plugins/maven-assembly-plugin/examples/sharing-descriptors.html – khmarbaise Jun 18 '20 at 13:41
  • This will not help me. There still will be duplication in module pom file. Only difference will be that instead of path to assembly I will use assembly id. So no big difference. I try to find a way which allow us to left module pom file without some adding of plugins. The module pom file should be as easiest as possible (maximal add some new dependency in case some JDBC) – Juraj Jun 18 '20 at 14:19
  • Did you find a solution for this ? – Omegaspard Aug 06 '21 at 12:32
  • No I did not find how to do it automatically in maven. Only workaround is that I created property which defined which phase the plugin should run. In my case the name is . Then in plugin I put execution option ${build.jar.package.phase}. By default it is package. If user know there are no dependencies then is set to None. In CICD we are able to do it automatically but I did not find the way how to do it directly in maven – Juraj Aug 09 '21 at 06:58

2 Answers2

1

I was able to work around this by adding a dummy folder: Can I create empty directories while packaging using maven-assembly-plugin?

<fileSet>
    <directory>${project.build.directory}</directory>
    <outputDirectory>empty_folder</outputDirectory>
    <excludes>
        <exclude>**/*</exclude>
    </excludes>
</fileSet>

This solution does not require any configuration outside of the descriptor, which makes it pretty clean.

I also have a multi-module project made up of sub-assemblies that get wrapped up in a master assembly. However some of the sub-assemblies are only valid on a specific OS (windows), which is simply denoted by having no content for the other OS'. An empty archive being produced by the sub-assembly is preferred in my case so that dependencies from the master assembly to all sub-assemblies are valid independent of platform. The master-assembly is uneffected by the empty directory since it doesn't match any of its assembly inclusions.

0

Yes, it works.

In my old configuration, It looks to include all files and folders at de ${basedir}/build/, and there is no output directory configured.