maven-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Eggers <its_toas...@yahoo.com.INVALID>
Subject Re: How to run jar dependent on maven repo jars? How to extract dependent jars into folder to include in classpath?
Date Mon, 11 May 2015 20:08:34 GMT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 5/11/2015 12:46 PM, Richard Catlin wrote:
> I built a jar using a pom.  The jar depends on jars in the maven 
> repository.  How do I run my jar and include the jars in the maven 
> repository in the classpath?  If it possible to extract all the
> dependent jars into a /lib directory that I can add to my classpath
> at runtime?
> 
> Thanks. Richard
> 

Richard,

I can think of two ways to do this.

1. Uber jar (I've not tried this)

See the Shade plugin at:
https://maven.apache.org/plugins/maven-shade-plugin/

2. Assembly plugin (this is what I do)

A. JAR Plugin
In your POM, configure the JAR plugin to contain three pieces of
information.

a) mainClass - this is obviously the class that has the main method
b) addClassPath - set this to true
c) classpathPrefix - I've set mine to lib

B. Dependency Plugin
In your POM, configure the dependency plugin to copy all of the
dependencies to the subdirectory of the target directory. The
subdirectory name should be the same as the classpathPrefix value

For example:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <version>2.8</version>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>copy-dependencies</goal>
      </goals>
      <configuration>
       <outputDirectory>${project.build.directory}/lib</outputDirectory>
       <includeScope>compile</includeScope>
     </configuration>
    </execution>
  </executions>
</plugin>

(This is an old project, update as appropriate).

C. Assembly Plugin

Configure the assembly plugin to create a zip file, tar.gz file,
whatever . . .

In src/main/assembly/assembly.xml, put in something like the following
(note that I've left out the namespaces for brevity):

<assembly>
    <id>bin</id>
    <formats>
        <format>tar.gz</format>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <includeSiteDirectory>false</includeSiteDirectory>
    <fileSets>
        <fileSet>
            <directory>target</directory>
            <outputDirectory></outputDirectory>
            <includes>
                <include>*.jar</include>
                <include>lib/</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

C. Construct your package:

mvn package
mvn assembly:single

This will leave you a .tar.gz file and a .zip file (using the
assembly.xml above).

D. Unzip (or untar) and run

Now when you unarchive your .tar.gz file or .zip file, you'll end up
with a JAR file (your code) and a lib subdirectory with all of the
compile time dependencies. You should be able to run the JAR file from
the command line with:

java -jar [your-jar-file-name]

I'm a relative newcomer to Maven, but I hope this helps.

. . . just my two cents.
/mde/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJVUQxCAAoJEEFGbsYNeTwtJ6MIAKdAktpSRvs8c+e8gxQSZ0Ce
zTGUi90FxH1tDS5BKhWVxAFH+UA6ZPA/rFl/UCG0S698ZDZ7CHBZbhp0AxDHZg8j
M9TvSOx0OauTiqubY4PmpYDE34oTSUVQPBvn7BQxgUa6PXLMNYTN+ex8OZzXpG7G
yWyU0IW5o1mHwo/zCOy7yetHPezRC0KGPiQc7esjpiubuwHWPOUebBNeiqD/0/Fg
iS0rIUDHOZaoryumYr9gFvRpQ0Tyorz/PT51f/6t0cceWNfgPls0yCxdQmzsaUVD
FA4Ck2fkIeAquRCmxdDOXtM5PKSVFj8Sd1O2OHkcYPMgCJm38GVd4fSfx17s+dU=
=MNr0
-----END PGP SIGNATURE-----

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


Mime
View raw message