0

I've tried to find a proper solution to my question, but it looks like it is similar to the following one maven assembly include the current project jar in the final zip/tar.

The idea of a project is to have one parent pom and a couple of child ones. I would like to have 'jar-with-dependencies' for each of child when I will execute 'mvn assembly:single' command from the root level.

So, what I have got so far:

  1. If I execute mvn package and mvn assembly:single one by one, then first will be completed successfully and the second one with a warning that child project was not included. Since my module has not been included, I am not able to launch the target.
  2. If I execute mvn package assembly:single, then required jar with all the dependencies will be created and I am able to launch the target.

I afraid that I have missed a configuration in one of my pom.xml. I will appreciate if someone could help me with that. I am adding a link on GitHub repository with this example.

btw, I'm using maven-assembly-plugin version 3.1.0

Thank you in advance, and I guess, I will need to buy a really good book about Maven.

mnlaptev
  • 113
  • 1
  • 6

2 Answers2

0

The key point is disabling assembly execution for parent project: it can be achieved combining <skipAssembly> option, profiles section and properties section.

With the following pom files, it works fine.

Parent pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.slemarchand.samples.jarwithdependencies</groupId>
     <artifactId>parent</artifactId>
     <version>1.0-SNAPSHOT</version>
     <packaging>pom</packaging>
     <name>parent</name>
     <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <skip.assembly>true</skip.assembly>
     </properties>
     <modules>
        <module>child1</module>
        <module>child2</module>
     </modules>
     <dependencyManagement>
        <dependencies>
           <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>4.11</version>
              <scope>test</scope>
           </dependency>
        </dependencies>
     </dependencyManagement>
     <build>
        <pluginManagement>
           <plugins>
              <plugin>
                 <artifactId>maven-assembly-plugin</artifactId>
                 <configuration>
                    <skipAssembly>${skip.assembly}</skipAssembly>
                    <descriptorRefs>
                       <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                 </configuration>
              </plugin>
           </plugins>
        </pluginManagement>
     </build>
     <profiles>
        <profile>
           <id>jar</id>
           <activation>
              <file>
                 <exists>${basedir}/src/main/java</exists>
              </file>
           </activation>
           <properties>
              <skip.assembly>false</skip.assembly>
           </properties>
        </profile>
     </profiles>
</project>

Child 1 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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.slemarchand.samples.jarwithdependencies</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child1</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>child1</name>

  <dependencies>

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>25.1-jre</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>

  </dependencies>
</project>

Child 2 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>

<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.slemarchand.samples.jarwithdependencies</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>child2</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>child2</name>

  <dependencies>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
    </dependency>
  </dependencies>

  <build>
  </build>
</project>

You can find the sample project here: https://gitlab.com/slemarchand/jar-with-dependencies-multi-module-sample.

0

Thanks for the answers. It turns out that if was misconfiguration in child pom.xml. I didn't add definition of the plugin under the plugins section: <build> <plugins> HERE <plugins> </build> So it was properly described under <pluginManagement> and it blinds me...

mnlaptev
  • 113
  • 1
  • 6