4

I have a maven project.

Inside that project, I have a .zip dependency that carries a jar and I need to extract that jar out of the zip dependency and have maven use the jar as a dependency. I can currently download and unpack the zip but, cannot figure out a way to add the unpacked jar as a dependency for the project during the build process.

Here is what I'm currently doing for unpacking:

<dependency>
  <groupId>com.bar</groupId>
  <artifactId>foo</artifactId>
  <version>${foo.version}</version>
  <type>zip</type>
</dependency>
...
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
      <execution>
        <id>unpack</id>
        <goals>
          <goal>unpack-dependencies</goal>
        </goals>
        <phase>validate</phase>
        <configuration>
          <includeGroupIds>com.bar</includeGroupIds>
          <includeArtifactIds>foo</includeArtifactIds>
          <outputDirectory>${basedir}/target</outputDirectory>
          <type>jar</type>
        </configuration>
      </execution>
    </executions>
 </plugin>

I read up on some other posts that you could try adding the jar to the class path using this.

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
      <additionalClasspathElements>
           <additionalClasspathElement>${basedir}/target</additionalClasspathElement>
   </additionalClasspathElements>
 </configuration>
</plugin>

Even doing so I was still unable to reference the packages in foo.jar in my project.

Can someone help me?

Trogdor
  • 41
  • 4
  • 1
    What have you tried to achieve your wanted results? What has your research concerning your problem shown? Can you provide code of your tries? [How do I ask a good question](https://stackoverflow.com/help/how-to-ask), [How much research effort is expected](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) and [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) might be helpful to improve your question. – Geshode Jan 19 '18 at 01:42
  • This is a little strange... Why are you downloading a zip that contains your dependency? Can't you just put the dependency inside the dependencies list? – R. Karlus Jan 19 '18 at 01:57
  • In the zip file there is a .dll file that is needed by the jar in order to run. I agree that it would be nice to just add the jar as a dependency in the dependency list. – Trogdor Jan 19 '18 at 03:02

3 Answers3

0

Some time ago, I've faced the same problem. I had a zip file as my dependency, and during the build process I need to extract it and separate the content inside my generated package.

I don't know what are you using to deliver your project, but at that time I've used the maven-antrun-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>2.6</version>
</plugin>

With this, I've used the tag unzip inside my target configuration. As you can see here or here. I just don't recommend you to use the task tag as they're using, you'd better prefer the target tag.

Hope it helps you.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
R. Karlus
  • 2,094
  • 3
  • 24
  • 48
0

For maven to use it without subtly breaking stuff elsewhere, you must install the jar into your local repository.

I would suppose that a combination of unpacking the zip file in target/ and then invoking install:install-file on the resulting jar could do what you need. I asked some years back how to integrate that in a normal build - you might find the answer relevant. Multiple install:install-file in a single pom.xml

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

Let's assume that, after unpacking the zip, you have foo.jar in your module's target folder: ${project.build.directory}/foo.jar

Having this in place, you can then declare a System Dependency pointing to that jar, e.g.

<dependency>
  <groupId>foo</groupId>
  <artifactId>foo.jar</artifactId>
  <systemPath>${project.build.directory}/foo.jar</systemPath>
</dependency>

Tip: if you dont want to delete/re-download the jar each time you do a clean (some IDE will complain the the jar is not always present) just download it once in the ${project.basedir}.

To download the jar once, you can put your "unpack" execution in a profile that gets activated only when the jar is missing.

<profiles>
  <profile>
    <activation>
      <file>
        <missing>${project.basedir}/foo.jar</missing>
      </file>
    </activation>
    ...
  </profile>
</profiles>
PaoloC
  • 3,817
  • 1
  • 23
  • 27