Opal
Opal

Reputation: 1057

Adding another project's jar as a resource using Maven

Within my project I have a sub-project auto-updater. Basically a jar file that is extracted and run when an update is available.

Is it possible to compile the sub-project, then place the outputted jar as a generated-resource so that the updater.jar is included in the final jar such as:

Project-1.0.jar
 |-updater.jar
    |-Main.class
    |-B.class

Thanks in advance for any help(I'm new to Maven)

Upvotes: 6

Views: 2496

Answers (3)

Hurda
Hurda

Reputation: 4715

This task is calling for maven-assembly-plgin or maven-dependency-plugin

(I expect that updater is also maven project) this shoudl be proper configuration for maven-dependency-plugin [I did not test this, you might also need to put updater to project depndencies]

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy</id>
            <phase>prepare-package</phase>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>
              <artifactItems>
                <artifactItem>
                  <groupId>company.com.project</groupId>
                  <artifactId>Updater</artifactId>
                  <version>0.0.1-SNAPSHOT</version>
                  <type>jar</type>
                  <outputDirectory>${project.build.outputDirectory}/classes</outputDirectory>
                  <destFileName>updater.jar</destFileName>
                </artifactItem>
              </artifactItems>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

Upvotes: 4

Ashay Batwal
Ashay Batwal

Reputation: 5613

Add the sub project to your parent project as follows

<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/maven-v4_0_0.xsd">

    <modelVersion>1.0.0</modelVersion>
    <groupId>Project-1.0</groupId>
    <artifactId>myproject</artifactId>
    <packaging>pom</packaging>

    <version>1.0-SNAPSHOT</version>

    <name>Project-1.0</name>

    <modules>
        <module>../updater</module>
    </modules>

    ...
</project>

Then in you updater project pom file make the following changes

<?xml version="1.0"?>
<project>
  <parent>
    <artifactId>myproject</artifactId>
    <groupId>Project-1.0</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <modelVersion>4.0.0</modelVersion>

  <groupId>Project-1.0</groupId>
  <artifactId>updater</artifactId>
  <version>1.0-SNAPSHOT</version>

  ...

</project>

When you compile your parent project, the child project will automatically get compiled.

Then to add the sub project jar in your jar, add the following plugin in parent pom.xml

 <plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>process-resources</phase>
        <configuration>
          <tasks>
            <copy todir="${project.build.directory}/lib">
              <fileset dir="${location of updater1.0.jar}"/>
            </copy>
          </tasks>
        </configuration>
        <goals>
          <goal>run</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

Upvotes: 0

Paulo Pedroso
Paulo Pedroso

Reputation: 3685

It would be nice if your little jar is also built with Maven. Then let's say your little jar POM's contains this:

<groupId>company.com.project</groupId>
<artifactId>Updater</artifactId>
<version>0.0.1-SNAPSHOT</version>

Then your Project-1.0 should use this dependency:

<dependency>
    <groupId>company.com.project</groupId>
    <artifactId>Updater</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

Upvotes: 0

Related Questions