Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a WAR project with unzipped JAR dependency?

I have two projects, my-lib and my-webapp. The first project is a dependency of my-webapp. Thus, when ask Maven2 to build my WAR, the my-lib JAR is added in the WEB-INF/lib/ directory of the web application.

However, I want to have the my-lib JAR unzipped directly in the WEB-INF/classes directory, exactly as if the my-lib sources were contained in the project my-webapp.

In others words, instead of having the following WAR content:

my-webapp/
  ...
  WEB-INF/
    lib/
      my-lib-1.0.jar
      ... (others third libraries)

I want to have that:

my-webapp/
  ...
  WEB-INF/
    classes/
      my-lib files
    lib/
      ... (others third libraries)

Is there a way to configure the my-webapp or the Maven2 war plugin to achieve that?

like image 598
Romain Linsolas Avatar asked Sep 02 '09 14:09

Romain Linsolas


People also ask

How do I create a Maven war project?

Using the mvn:war:exploded command, we can generate the exploded WAR as a directory inside the target directory. This is a normal directory, and all the files inside the WAR file are contained inside the exploded WAR directory.

How do I package a jar with dependencies?

First, <shadedArtifactAttached> marks all dependencies to be packaged into the jar. Second, we need to specify the transformer implementation; we used the standard one in our example. Finally, we need to specify the main class of our application. The output file will be named core-java-0.1.

Does jar file contain dependencies?

the executable jar should NOT contain the dependencies, just enough of your own code to run, with the dependent classes coming from the before mentioned classpath set up in the manifest.


1 Answers

As blaufish's answer says, you can use the maven-dependency-plugin's unpack mojo to unpack an artifact. However to avoid the jar appearing in WEB-INF/lib, you need to not specify it as a dependency, and instead configure the plugin to unpack specific artifacts.

The following configuration will unpack the contents of some.group.id:my-lib:1.0:jar into target/classes during the process-resources phase, even if the artifact is not defined as a dependency. Be careful when doing this though as there is potential to clobber your actual content, this can cause much debugging.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack-my-lib</id>
      <phase>process-resources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>some.group.id</groupId>
            <artifactId>my-lib</artifactId>
            <version>1.0</version>
            <type>jar</type>
            <overWrite>false</overWrite>
          </artifactItem>
        </artifactItems>
        <outputDirectory>${project.build.outputDirectory}</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
      </configuration>
    </execution>
  </executions>
</plugin>
like image 67
Rich Seller Avatar answered Sep 21 '22 12:09

Rich Seller