Deploy gradle to directory - java

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.

Related

Maven: How to add all jar files in lib folder during a web application build

I have an old project. I am trying to add maven build to the project.
All the jar files in the project are present currently in the WEB-INF/lib folder.
Is there a way to add these jar files to the classpath during source compilation and then have it in the war file lib
This is one of the places where switching to Maven hurts. There are a lot of suggestions where most short-circuit something you will need later, and hurt you there. I would suggest that you
Move your jar files out of the lib folder into another project folder not meaning anything special to Maven.
For all jars that you easily recognize, make the proper Maven dependency in your pom file. This will allow Maven to download sources and javadoc if present.
For the remaining jars, you can tell Maven to install them as part of your normal build as custom dependencies. I asked the same question years back and got a very useful response at Multiple install:install-file in a single pom.xml. This will allow you to get up and running quickly.
When you have the time, locate proper replacements for your custom dependencies.
Take your time doing this. It is tedious work but it pays off quickly.

Make an executable JAR from a maven project

I have a question about using Apache Maven: I built a very simple Maven-based project. This works fine so far.
Now I want to make an executable JAR file from my target file. The problem here seems to be that the dependencies (external libraries) are not packaged together with my app.
I've already googled and found the maven-assembly-plugin. That actually does exactly what I want, but seems to be somewhat inflexible, since the dependencies are not automatically resolved and I can not specify file filters, etc. (or only with much effort over assembly.xml).
What I really like is the solution of spring-boot-maven-plugin, so automatically determine all linked JAR files and put in a lib folder. Now my little project is not a Spring application, so Spring Boot might not be suitable for me, right?
So what would interest me: Is there a way to get a similarly structured and executable JAR archive as spring-boot-maven-plugin builds?
Just use Maven Shade Plugin. It packages your dependencies inside your jar and you can specify the Java packages to exclude

Adding plain Java project as a classpath to an eclipse plugin

I have a plain Java project (not a plugin project) which I want to add to a classpath of a eclipse plugin which I am developing. But in web projects I can add that project as a build path and it works fine. But I tried same thing in eclipse plugin, I am able to compile successfully, but at run time I am getting java.lang.ClassNotFoundException.
I know OSGi quite well and I know how to add OSGi into an classpath (using export-packages) but what I want is to add Standard, non-osgi project into an classpath, so that I wont' get runtime errors. Is there anyway I can achieve this?
I can export project as a jar file or make it as a plugin project and it would work fine. But that's not my option currently because, still that API is in pre-alpha stage, and there would be lot of changes going on. So I am trying to avoid pain of exporting it as jar file everytime. Is there any option for me other than this?
I have a similar situation: I want non-OSGi Maven dependencies integrated into the classpath of my plugin. I succeeded with a roundabout solution, which I think is the best I could get.
I have a build step outside of Eclipse where I copy the class files of the dependency into the plugin's lib folder. The lib folder is specified in MANIFEST.MF as an entry in Bundle-ClassPath and (here comes the hack) as a source folder in build.properties. That was the only way to make the plugin work both when launched from within Eclipse and when exported.

Eclipse - External JARs and git

I am currently using eclipse for working with Java. Additionaly I use git to synchronize my project between my laptop and my desktop PC.
The problem is now the following: I added external JARs to the project (Slick-Util, LWJGL).
But the path to each library is another on each device. So everytime I start working on the other device, I have to change the path to the jar files and the javadocs.
The libraries are all stored in my eclipse workspace. So the libraries and the projects are all in the same folder. And this folder is also commited with git.
Is there a way to change the eclipse settings (or do something else) so I do not have to change the path to the libraries and javadocs everytime?
I already googled and searched for it, but I could not find something about it.
Just don't add the libraries' jars to git. There are multiple build tools for java, which manage dependencies for you - you just state the libraries you're going to use, and the build tool downloads it for you at build time.
I would recommend Gradle, but Maven is also a very popular choice.
In gradle, you would create a file build.gradle, and define your dependencies in it:
apply plugin: 'eclipse'
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.lwjgl.lwjgl:lwjgl:2.9.0'
compile 'org.lwjgl.lwjgl:lwjgl_util:2.9.0'
}
Then you would run gradle eclipse from the command line - that would add the libraries you use to the classpath in eclipse. And when you want to compile and package your project you would run gradle build from the command line. You should read about it if you're going to use it, what I describe may not be exactly what you need.
Also, there are instructions for using LWJGL with maven.
add jar files to a lib folder inside your project like this : D:\Workspace\myproject\lib\your-jar-file.jar
then go to your projects build path select libraries tab and click on add jars and NOT add external jars this way your jar files path will be relative to your project
EDIT :
I highly recommend to use a build tool as Kiril Raychev described.
it will look a bit confusing to start with but after a while and after a normal growth in your application that will lead to using different frameworks, controlling and managing dependencies and their conflicts without a build tool will literally kill you.
You can simply use -f flag on add command.
git add -f test.jar
And, then commit and push to your repo.
Up until now i usually use svn so i'm not entirely sure how it works out in git, but have you tried to store the JARs in the lib folder of the project they are used in? (Eclipse displays the lib folder so you can easiely add them to the buildpath with a right click on the library in the package explorer.)
That way the relative location/path of the libraries to the project should stay the same. Furthermore if you plan to pack the project into a JAR later you ship the libraries inside that JAR without having to worry whether the user of that file even has them on his computer.
PS: Looks like i'm a minute too late. Dave basically said the same thing.

package all external classes in my jar, with Eclipse

I am working on a Hadoop project in Eclipse that depends on another one of my projects; I've included the other project in my build path, but when I export the dependent project, it only contains the classes from that same project.
Ordinarily, this would not be a problem, as I could just link the other project with the -cp flag, but Hadoop requires you to pass the jar as an argument, meaning that all of my dependencies must be inside that jar.
Is there a way, in Eclipse, to automatically build and include classes from projects that you depend on?
Thanks.
You coud use Ant to automatically build, test and export. It needs some time learning it, but its worth.
There are possible tasks (fileset, zipgroupfileset, copy) to include files, jars (unzipped) or anything into the final jar. By this way you definitly know whats inside your distribution jar and you don't need an eclipe installation running.
I suggest you take a look at maven as a build tool. You define the dependencies and build steps for each of your projects in files called pom files. The maven plugins for Eclipse (the m2e plugins) can take the configuration in the pom file and setup your Eclipse build paths and project description so that you can access the classes in your other project in Eclipse. Maven can also create a jar for you that has the classes from both projects (a "jar-with-dependencies").
In maven terms, your two projects are called "artifacts" with one having a dependency on the other.
The one downside to maven (and the cause for many negative comments about maven) is an initially steep learning curve that can be frustrating. What you're trying to do, however, is very straightforward and I expect you can find a number of examples showing you exactly what you want to do.
The first step, and that's what my answer is about, is to take a look at maven. It may seem overly complex, but it can scale to handle just about any build configuration you need as your hadoop apps get more and more complex.
You can export a project as a Runnable jar, which can be useful if you want a single jar, with dependencies included.
Select the Project. File > Export. Select the Java section. Select Runnable JAR file.
See related answer:
Eclipse: How to build an executable jar with external jar?

Categories

Resources