1

I have a project which has multiple .jar files in it. To add this as a dependency, I need to turn all these jars into one big jar. So far I have come until:

    <?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>mainProjects</groupId>
      <artifactId>mainProjects.master</artifactId>
      <relativePath>../pom.xml</relativePath>
      <version>1.0.0-SNAPSHOT</version>
   </parent>
   <groupId>mainProjects</groupId>
   <artifactId>sampleModule1</artifactId>
   <name>sampleModule1</name>
   <version>1.0.0.qualifier</version>
   <build>
    <plugins>
     <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                    <includes>
                        <include>Sample1.jar</include>
                        <include>Sample2.jar</include>
                     </includes>
                    </artifactSet>
                    <finalName>${project.artifactId}-${project.version}-final</finalName>
                    <outputDirectory>${project.artifactId}/build</outputDirectory>
                    <shadedArtifactAttached>false</shadedArtifactAttached>
                </configuration>
            </execution>
        </executions>
    </plugin>
    </plugins>
</build>
</project>

This creates the final jar, however, inside there are no content from the other jars (sample1.jar and sample2.jar) as there should be. I have looked into the documentation of the plugin, however all they did was to do it via class files, not jars. So I don't know how to proceed from now on.

Any thoughts?

Update:

So in order to make it clear, hereby I share the project structure that I have:

 +- mainProjects
 |   +- pom.xml
 |   +- mainProject1
 |   |  +- pom.xml
 |   |  +- src
 |   +- mainProject2 
 |   |  +- pom.xml
 |   |  +- src   
 |   +- group1
 |   |  +- pom.xml  
 |   |  +- sampleModule1
 |   |  | +- pom.xml  
 |   |  | +- build
 |   |  | +- sample1.jar
 |   |  | +- sample2.jar
 |   |  | +- sample3.jar
 |   |  | +- sample4.jar
 |   |  | +- sample5.jar
 |   |  | +- sample6.jar
 |   |  +- sampleModule2
 |   |  | +- pom.xml
 |   |  | +- src

Now, I want to be able to use sampleModule1 as a dependency in the pom.xml of mainProject1 as a jar, like this:

      <dependency>
         <groupId>group1</groupId>
         <artifactId>sampleModule1</artifactId>
         <version>1.0.0.qualifier</version>
         <scope>system</scope>
         <systemPath>sampleModule1/build/${project.artifactId}-${project.version}-final.jar</systemPath>
   </dependency>

to achieve this, I need to compile all the jars into one jar, so that I can add it by using one systemPath. I found this which shows an example of how to include multiple files into one. However, in the example they are not jars, but rather classes and others. Now here, I am trying to achieve the same, but with only jars.

Community
  • 1
  • 1
Schütze
  • 1,044
  • 5
  • 28
  • 48
  • what is your project structure? are these jar files outside pom.xml file? – Apostolos Jul 01 '16 at 09:51
  • Ah thanks for reminding. They are in the same directory with the pom.xml. – Schütze Jul 01 '16 at 09:53
  • why dont you add them as dependencies in pom.xml? that's the meaning of maven! – Apostolos Jul 01 '16 at 09:54
  • 10 .jars under the same project, are you sure? I mean, as far as I know while adding dependency, you are allowed to add one .jar per artifact-id. Can I just use the same artifact-id and keep adding all the jars? – Schütze Jul 01 '16 at 09:56
  • please post a screenshot of your structure and your actual jar files to help us better understand. thnx! – Apostolos Jul 01 '16 at 09:56
  • @Apostolos Done, kindly check the question again. Thanks! – Schütze Jul 01 '16 at 10:36
  • You have been told a few times during the past two days to _not_ use `system/`. Why are you still ignoring these advices? What are these `sampleX.jar`s good for? There is a `build.xml`. Are you migrating from Ant/Ivy? If yes, I strongly recommend to abandon this Ant/Ivy approach of copying dependency artifacts into each and every project. Otherwise you are more working against Maven rather than with it. Have you already read the docs I linked you recently? – Gerold Broser Jul 01 '16 at 10:37
  • i agree. scope system should not be used. install these jars in local repo if needed and reference them inside your pom.xml not as relative system paths. – Apostolos Jul 01 '16 at 10:40
  • I have 3 issues regarding maven and this is one of them. I didn't know that `system` shouldn't have been used **at all in any case**. Now that you told me for this as well, point taken. Now, is my problem here related to `systemPath` issue? Nevertheless, yes I am migrating from Ant to maven. I read the first link (repositories) and the second link is a complete book, which I cannot read in such a short time. – Schütze Jul 01 '16 at 10:45
  • @Apostolos I cannot use terminal commands as the project is supposed to work on a Jenkins machine on-the-fly, so using that `install` command and installing these jars are out of the option. The question stands: is there a way of creating one jar out of multiple jars? That's it. – Schütze Jul 01 '16 at 10:48

1 Answers1

2

There are two ways to solve your problem! If you just want to to add files into your jar, you can use the resources tags to add them

<build>
    <resources>
        <resource>
            <filtering>false</filtering>
            <directory>lib</directory>
            <includes>
                <include>*.jar</include>
            </includes>
        </resource>
    </resources>
</build>

This now puts all jar files from you lib folder into the main jar. The root folder is the folder from which you invoked the corresponding pom.xml. With this you can add arbitrary files to you jar. For a complete syntax reference take a look here.

The other way and maybe the most convenient way is to use the maven-shade-plugin this allows you to copy almost everything into you final jar.

<?xml version="1.0" encoding="UTF-8"?>
<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-shade-plugin</artifactId>
   <version>2.4.3</version>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
            <goal>shade</goal>
         </goals>
         <configuration>
            <artifactSet>
               <includes>
                  <include>groupid:artifactid_a</include>
                  <include>groupid:artifactid_b</include>
               </includes>
            </artifactSet>
            <filters>
               <filter>
                  <artifact>*:*</artifact>
                  <includes>
                     <include>resources/*.png</include>
                  </includes>
               </filter>
            </filters>
         </configuration>
      </execution>
   </executions>
</plugin>

The artifactSet parts allow you to reference jar from within you local repro to be included just by mentioning their groupid and artifactid

This will include the content like class files, manifest etc. and also the folder structure) of the mentioned artifact (from you local repro) into you jar.

If you want to include other arbitrary files into you jar you can use the filter tag of the plugin which allows you to specify files and file-patterns directly. For a complete syntax reference take a look here.

P.S.: If you want to exclude certain files you can replace the include tag by an exclude tag ;-)

Westranger
  • 1,308
  • 19
  • 29