When I run gradle clean jar it pulls down all my executable JAR's dependencies under ~/.gradle and then creates my JAR.
I am then having to jump through serious hoops to run the JAR locally:
First I create a lib directory in the same folder as the newly-created JAR
Then I have to cherry pick all of my project's transitive dependencies (there are lots) out from ~/.gradle and copy them to lib
Then I run from the command-line java -jar myapp.jar -cp "lib/*"
Every time my dependencies change (new code is added, etc.) I have to go through this process, and it makes me wonder if there is a more elegant way to run your Gradle-built apps locally.
I ended up going with the application plugin. It turns out that Gradle considers compile dependencies a subset of runtime so I didn't need to change any dependency scopes. I can now just issue gradle run and it runs my app beautifully.
Related
I'm trying to get a maven managed project to run on the command line.
I have a set of dependencies in the pom.xml which are subsequently downloaded and installed in the ~/.m2/repository/. I've included the necessary config in my pom to add the classpath to the jar manifest.
Now the problem is i'm attempting to run the jar thus: java -jar project-SNAPSHOT.jar.
Java can't find the downloaded dependencies (i'm assuming because they are listed without paths in the manifest?) , but i'm not sure how best to get this running.
Options 1:
The jar created does not have the dependent jar files. So, you need to tell java the class-path where all the dependent jars are
java -cp /lcoation/of/dependency1.jar:/location/of/dependency2.jar:/location/of/dependency3.jar -jar project-SNAPSHOT.jar
Option 2:
The easier and much better solution is to use AppAssembler plugin. What it does it packages your jar in a directory structure that contains
dependent jars
the created jar
shell/windows scripts to execute it
have a look here http://www.mojohaus.org/appassembler/appassembler-maven-plugin/
Option 3:
If you do not want all the baggage and just wanted to have one jar-with-dependency
You may want to refer here How can I create an executable JAR with dependencies using Maven?
This will contain all the dependent jars within it.
Edit 1: For Option 1, Brad M mentioned that you can get a list of all your project's deps using the dependency plugin. dependency:build-classpath
mvn exec:java -Dexec.mainClass="com.vineetmanohar.module.Main" -Dexec.classpathScope=runtime
You can find more examples here: 3 ways to run Java main from Maven.
This may have been asked to death, but I can't find an answer. I have a pretty simple java project (not web), its built with ant with sources located in ./src and dependencies in ./lib. Looking to modernize it to either maven or gradle. Since I've had good results with gradle and android, decided to go with gradle. That means I'll be dropping ./lib for dependency management.
However, I cannot figure out how to use grade to deploy the project. I would like to deploy manually for now. So I would need to have jar build from the sources and having all dependencies copied into lib (or whatever) directory where jar is.
So far... I'm getting nowhere quickly.
Gradle Application plugin is perfectly suited for this use-case.
the task distZip will create a deployable zip file complete with dependencies.
Let's say we have a simple Java project compiles into a simple.jar with its POM depends on log4j.jar. When I open the simple-1.0.jar, inside there is no log4j.jar. And then we upload this simple-1.0.jar onto Nexus.
Now when deploy this simple-1.0.jar to the target server, how do we deploy the log4j.jar?
Thanks
Jirong
You can either package the dependencies with your application (i.e. create an uber-jar, WAR, or EAR file) or you can deploy the dependent jars to a location on the server and then set that location as the classpath when you run the jar.
Packaging an application as an Uber-JAR
One way of creating an uber-jar with Maven would be to use the maven-shade-plugin
Deploying the dependent jars and setting classpath
In this scenario you would create a "libs" folder somewhere on the target machine and copy all of the dependent jars into this folder.
Then when you launch your application you would set the classpath like so:
java -classpath /{libs directory} -jar simple-1.0.jar
Multiple classpath entries can be specified by separating them with a : like also:
java -classpath /{directory1}:/{directory2} -jar simple-1.0.jar
You can have Maven list the resolved dependencies using the dependency plugin:
mvn dependency:list
I have downloaded an Eclipse project and I want to be able to have other people compile it without using Eclipse. It is a fairly large Java project that is still being worked on. How would I make a compile script that compiles like eclipse?
I highly recommend you look into Maven. Basically you'll define a Maven pom file in the root of your Eclipse project directory which will contain your dependencies (jars) as well as compile and assembly configuration. With this in place you can simply checkout a project and run a maven build command against the local directory you checked out to and an executable/deployable package will be created.
I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It compiles, and if I run the program with no options, it executes fine, displaying usage information. However, if I actually pass in the parameters, it fails and says NoClassDefFoundError: com/sas/isd/midasapi/ParticipantDetailExt, which is in a jar I included as an external jar. I am confused that it compiled and runs to show usage information, but it fails to find the class after as the ParticipantDetailExt is a class that is imported. Wouldn't it identify that it was not found during compiling? How do I get it so that my on jar with ParticipantDetailExt is seen when I run my exectutable jar? Is there a classpath thing or pom thing I need to do in addition to the adding jar as external jar?
I assume that you are running mvn clean package or mvn clean install to create your jar.By default the jar created by a maven project does not include dependencies in that jar.
Option 1# create a jar-with-dependencies, see: How can I create an executable JAR with dependencies using Maven?.
Option 2# If you are just looking to copy dependencies to a lib folder see: http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html