Generate Eclipse launch configurations with Gradle - java

We use Eclipse to launch and debug our Java GWT project. We have a couple of eclipse launch configuration files with many classpath entries needed for running the project.
Previously we used ant to build our project and had all our dependencies present on our local storage, with all classpaths pointing to libraries explicitly for project as well as launch configuration classpaths.
Once we migrated over to Gradle 6.6 and used public repositories to download our dependencies, almost everything worked smoothly - all our project classpaths were generated correctly by the gradle build. However, our eclipse launch configurations still contain the old hardcoded classpaths and I'm unable to find a way to generate these launch configurations using those gradle configured classpaths. As a result we've had to maintain the dependencies on our local filesystem so that the configurations have access to them in order to debug and run our code.
Is there a way Gradle can be used to generate these launch configurations so that we can get rid of the libraries on our filesystem and rely on whatever it pulls from the repositories during a build?
Any suggestions or workarounds will be appreciated.

It you're using Eclipse's built-in Gradle tooling (aka, Buildship) and let Eclipse generate your launch configuration, it will automatically be in sync with the project's build path, which is automatically kept in sync with the dependencies declared in your Gradle build file.
Here is an example Gradle project I just created, where I added some dependencies and the launch configuration "just works." The project is named "lib," notice under Classpath Entries is "Project Dependencies"; that's where Eclipse's Gradle tooling maintains and syncs with what's in the build.gradle file.
Here's what the project's Build Path looks like if I inspect it:
Whenever you add a dependency to build.gradle, it will automatically be included there; the launch configuration(s) then reference the project dependencies so the launches are also always in sync.
You might need to delete your existing launch configurations and let Eclipse generate new ones to get this.

Related

Use build.gradle for deployment

I'm learning how to use gradle to build my java applications. Currently I'm using eclipse with buildship plugin. I have it building my JARs and WARs, and also have gradle running my JUnit and Selenium tests. I like that it pulls in the dependencies I need as I need them during development.
It seems like it would make sense if my build.gradle files define my dependencies to build and run my application in dev then I should be able to use them for deployment. Otherwise I have to retrieve all my dependencies by some other means and deploy to my production environment, and managing 2 different methods of retrieving and deploying dependencies seems to be a risk for problems.
Can I use gradle or at least my build.gradle files in some way for my deployment?
Take a look at the gradle distribution plugin. This plugin adds tasks to create an "install folder" or an archive file (zip or tar) containing all the dependencies you'll need to execute/deploy your application.
Gradle application plugin also generates shell/bat scripts to invoke your application.

Find unused gradle runtime dependencies

I am in the process of updating a large set of legacy java applications. The current builds use ant with its dependencies coming from each project's lib directory. The dependencies are part of each project and checked in to source control. The purpose of the updates is to convert the builds to gradle and use a maven repository for dependency management. For simplicity, the legacy builds copied dependencies from other project's lib directories using an ant construct like:
<fileset dir="../anotherlegacyproject/lib">
<include name="**/*.jar" />
</fileset>
This approach is used for both compile and runtime dependencies. While this works, it leads to bloat of the deployment artifacts because of the many jars included which are not necessary at runtime. For example, many of the projects include junit, hamcrest, and jaxb-xjc in the runtime even though they are only used for compile or test. Most of the projects create both a zip file and an RPM containing deployable applications. I would like to only include the required runtime dependencies in the RPMs.
I am trying to determine a reliable method for identifying these unused runtime dependencies.
I have reviewed the post: How to find\remove unused dependencies in gradle but this does not address unused runtime dependencies. I have tried using the following gradle plugins, none of which identify unused runtime dependencies:
github.com/nebula-plugins/gradle-lint-plugin
github.com/wfhartford/gradle-dependency-analyze
docs.gradle.org/current/userguide/jdepend_plugin.html
I am open to any solution that will reliably work, but my preferences would be in this order:
An existing gradle plugin
Code that could be incorporated into the build.gradle file or groovy code packaged as a plugin
Anything else
Recently we migrated some project from 1.6 to 1.8, needed to find out unused jar. I was trying to use some plugin and tool but could not solve my issue.
Over stack overflow I got some insight to fix this issue. Below are the steps to find out unused jar in project.
First you need to take latest EAR/WAR of your module. Suppose you
module name is CheckDependency, so take latest EAR/WAR and get it
out lib folder.
Now in eclipse replace lib folder of your module with new one, which
taken from EAR/WAR.
Eclipse go to your module and select Gradle(STS)[This should be
installed in your machine]-->Hit Task Quick
Launcher-->Select projectTattleTaleReport It will generate
all report in you target folder with name tattleReport

Automatic library download in Eclipse with Gradle

I'm developing an java application. I'm using eclipse Luna and Gradle as my build-system. I can define dependencies in my build script and they get downloaded on a build. That's no problem... But how can I tell eclipse, that it should automatically download and add the dependency to my build path?
I wan't to use auto completion and so on, without manually downloading an jar and copy it to the project.
Is it possible?
Regards
Marc
You need the Eclipse Gradle tooling, which adds lots of Gradle-related functionality. Most importantly, it manages your Eclipse project build path to match the build.gradle dependencies.

How to specify dependency on tomcat libraries with gradle

A bit of background about my knowledge level: I'm currently trying to learn how to build a project with gradle. So far I don't have much experience with build tools (almost none). I do understand the general idea and have seen ant and maven files before but never written them myself. Until now I just used other peoples build scripts or configured my builds using Eclipse. So I might be on a completely wrong track here, if so please point me in the correct direction.
Also, I sadly don't have much experience building jars by hand.
I have an Eclipse project which I want to compile into a jar. Required library jars are on my local file system. I managed to make gradle use these via
dependencies {
compile fileTree(dir:'lib', include:'*.jar')
}
in addition I have my source files in src/main/java and just use apply plugin: 'java' in the build.gradle file. Trying to build the project with gradle build seems to do the right thing, as far as I can tell.
However, the library is supposed to be used in a web project running on a tomcat and makes use of some libraries that are supplied by tomcat, as far as I understand. E.g. I'm using javax.servlet.http.HttpServletRequest.
The project works fine in Eclipse, but there I have the tomcat library added to my Eclipse build path. When I check in Eclipse I can see that javax.servlet.http.HttpServletRequest is part of the servlet-api.jar which is part of the Tomcat library.
Now, when I build the project I get build errors because the java compiler cannot find the class because I didn't specify the servlet-api.jar in the dependencies. I guess I could download it somehow (or learn how to specify it as an external dependency to make gradle download it from some repository) but I am not sure whether that would be correct.
Is there a way to tell gradle to use the same library that Eclipse uses? Or some other general way to tell it about all the tomcat jars, the same way I can simply add the complete Tomcat library in Eclipse?
Or do I actually need another copy of these jars somehow and have to specify each one individually?
Also, do I need to add these library jars to my build-result library jar? As far as I know I need to add any jar I depend on to the resulting jar as well. But then again, I have read somewhere that some libraries are supplied by tomcat itself so they would have to be part of any war deployed on it.
I'm afraid, I'm confused by the combination of how to build a jar-file to be used in a war-file to be deployed on a tomcat using gradle and I don't know from which of these parts my problems originate. I hope someone reading this can help me untangle my thoughts and point me in the right direction or at least tell me how to add the jars included in the Tomcat library to my gradle dependencies.
With Gradle, whenever you add files or directories as dependencies, they are not treated as full-fledged artifacts (with group, name and version), but rather as simple files containing classes. It means that Gradle will not perform any conflicts resolutions on them, or pull transitive dependencies.
For you, just to get started, I recommend just to add tomcat dependency. Make sure it is the same version as the one in Eclipse.
apply plugin: 'war'
repositories {
mavenCentral()
}
dependencies {
providedCompile 'org.apache.tomcat:tomcat-catalina:7.0.47'
}
Also, look into Eclipse Integration Gradle project as a long-term solution.

How can I retrieve the classpath string from my Eclipse project that uses Maven?

How can I retrieve the classpath string from my Eclipse project that uses Maven? In eclipse , I have more than 100 .jar files imported via Maven and refrences as a library called "Maven Dependencies". How can I retrieve the "effective" classpath in the form of a string in my project?
The .classpath file does not reveal this.
You can try to use the java development toolkit (JDT) in Eclipse. The class JavaCore in the org.eclipse.jdt.core plugin can take a reference to your java project (an IProject) and return an IJavaProject which adds the behavior of knowing stuff about the classpath. Maven provides a classpath container that should also collaborate with the IJavaProject to answer back the actual source folders and jars on the classpath. Can't provide with any more info off the top of my head, but I think this approach is your only hope.
Unless you write an eclipse plugin that extends the maven plugin. Documentation is sketchy at best, but in theory you can write an extension that is asked to tweak the contents of the maven container in your project's class path. If at that time you were to cache information about the container contents off to your plugin's state folder, you'd be able to reference it when the user wants you to invoke your script. This extension would be invoked whenever maven is asked to update project configuration or dependencies.
I understand that you want to execute that script yourself and you have Maven installed. I guess the easiest way to run your application in this case is to use the Maven Exec Plugin:
http://mojo.codehaus.org/exec-maven-plugin/
(I'm usually using the exec:exec goal to run the application in its own process.)
Edit:
Otherwise have a look at the Maven Dependency Plugin to prepare the classpath if you don't want to use the Maven Exec Plugin:
http://maven.apache.org/plugins/maven-dependency-plugin/

Categories

Resources