Include plain java projects as dependency at gradle project - java

I have two projects
one plain java project and one gradle spring project created from jhipster
I would like to use the java project alongside with the gradle one.
After search on the net i have found how to do it link1 but this works only if all my projects are gradles.
Also when i try to add module dependency from Project Structure does not work.
Project tree
workspace
-project1
--build.gradle
-project2
What is the right way of doin this?
Also why i cannot make changes from IDE's project structure and automatically update build.gradle and settings.gradle with the new dependencies?

Change the plain java project into a gradle project, change the two projects into a gradle multi-project.
It's not hard to do, the plain java project probably just needs an almost empty build.gradle file and the settings.gradle file just needs to include it.
An IDE cannot help you much in these tasks, you will have to do most of it manually.

Related

Purpose of POM file dependency [duplicate]

I have just started working with Maven in Eclipse.
I tried running a sample program in it, and I encountered some problems. The errors were in my pom.xml file. What is the significance of the pom.xml file in Maven?
In short the pom.xml will have all information to build your project.
For example you want to build a project and you have only the pom.xml sent to you via mail. If there are enough entries in the pom.xml then that is all you need! You can import it to Eclipse, Maven will download your source code from CVS, download various dependency jars (like Spring, Apache Commons), run your test cases, build the jar/war, deploy to your jboss/app server, generate a report of your code quality (using Sonar, maybe). Each task you want to do will be mentioned as a goal.
The links already provided are good enough for reference.
POM is an XML file that contains the project configuration details used by Maven. It provides all the configurations required for a project.
POM means Project Object Model, and, as the name suggests, it defines the model of the project as well.
In the normal project development you will add JAR files and libraries as required. In Maven-based development, those JAR files, libraries are added to the project using this pom.xml. In the pom context we call those JAR files, libraries as dependencies.
Maven is a build tool and pom.xml is the main file for the project.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a single configuration file that contains the majority of the information required to build a project in just the way you want. The POM is huge and can be daunting in its complexity, but it is not necessary to understand all of the intricacies just yet to use it effectively.
For more reference, check Maven in 5 Minutes.
POM stands for project object model. It's the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used to build the project. It downloads required libraries easily using POM XML tags.
When there is no Maven, it needs to add all the library JAR files one by one to the project. But when there is Pom.xml there is no need to add library JAR files one by one.
Simply add the dependency to the Pom.xml, and it will automatically add the library JAR files to the project.
pom.xml is a file which describes the project, configures plugins, and declares dependencies. The POM names the project, provides a set of unique identifiers (called coordinates) for a project, and defines the relationships between this project and others through dependencies, parents, and prerequisites.
A POM file can include a modules section, which tells Maven which directories have POM files which need to be built.
In the build section you can define plugins for which you need to build the artifacts in your project.
Pom.xml is part of your maven project, using pom.xml, maven life cycle you can achieve it.
The pom.xml is a project object model which tells everything 3rd party tool dependencies and library's and required plugins it will give everything to your project like project means any java based web content like itself. once you create the maven project you will get the pom.xml blog which is everything to handle your project
If you want to test your project, you need to add testing dependencies which is the maven community it will provide you once added it will have everything tested.

Eclipse: reference local code instead of cached gradle dependency

I have two gradle java projects imported into Eclipse, one being a dependency of the other.
I would like for Eclipse to use the local dependency code, instead of the compiled dependency in the gradle cache folders, so I can modify and debug both projects simultaneously.
How do I force Eclipse to use a local dependency code?
The feature you are referring to is known as Composite Builds:
Importing into the IDE
One of the most useful features of composite builds is IDE integration. By applying the idea or eclipse plugin to your build, it is possible to generate a single IDEA or Eclipse project that permits all builds in the composite to be developed together.
In addition to these Gradle plugins, recent versions of IntelliJ IDEA and Eclipse Buildship support direct import of a composite build.
Importing a composite build permits sources from separate Gradle builds to be easily developed together. For every included build, each sub-project is included as an IDEA Module or Eclipse Project. Source dependencies are configured, providing cross-build navigation and refactoring.
The most simple way of achieving this is to use includeBuild in your settings.gradle.
rootProject.name = 'my-composite'
includeBuild 'my-app'
includeBuild 'my-utils'
With that in place, there's no need to configure the build path manually in Eclipse.
Gosh this was a trivial one and it took me way too long to figure it out.
Solved the issue by creating a file named external-projects.properties in the root folder of the parent project with the relative or full path to the dependency project folder.
To be precise, for me the folders tree ends up like this:
git-folder/
|__parent-project/
|__external-projects.properties
|__build.gradle.kts
|__rest (src/main, etc)
|__dependency-project/
|__build.gradle.kts
|__rest (src/main, etc)
And the contents of the external-projects.properties file:
dependency-project = ../dependency-project
The dependency project must also be in the build path of the parent project (done by "Right-Click project > Build Path > Configure Build Path" and adding it in the "Projects" tab).
After that, a "Right Click > Gradle > Refresh Gradle Project" did the trick.

IntelliJ Idea with Gradle: Best Practices

I am developing a Java application using IntelliJ Idea 14.1.4.
If it would have been solely Java application, I would have known exactly how to structure the project in Idea:
A single Java project, containing several modules: One for each part of the application (JAR).There will be at most 4-5 JARs.
The dependencies between the modules are also known: Protocol does not depend on anything, everything else depends on Infrastructure, and so on.
Next, I would like to use Gradle scripting for managing the project. So my question is what is the best practice to structure the code in Idea?
Should I create a single Gradle Project and a Gradle module for each of the JARs?
Should I create a single Java project (or maybe empty project) and Gradle modules for each of the JARs?
Should I create a single Gradle project and each of the modules will be a Gradle's sub-project? Maybe it will be better to have an empty project and several Gradle modules because not all of the JARs are closely coupled?
Since I have never used Gradle before, I would appreciate any guidance for the best practices when combining both Gradle and Idea.
Thanks,
Guy
As long as possible, I would keep the code in one source repository. On the root, I would have an "empty" project not outputting anything. All your jar projects would be sub-projects (in Gradle terms). You include them via the settings.xml file located in the root project.
Each sub-project has its own build.gradle file. In those files, you can easily define the dependencies between your sub-projects, e.g.:
dependencies {
compile project(':subProject3')
}
For convenience, I often create a special export task to put all artefacts in one export/ folder on the root level so that you don't need to go through all those sub-folders to get your stuff.
task export(type: Copy) {
from project(':subProject1').jar
from project(':subProject2').jar
from project(':subProject3').war
into 'export/'
}
IntelliJ Ultimate 14 works fine with this approach. You can simply hit Make to compile everything. You might also want to configure your project settings to run gradle jar or gradle export during a make if you prefer.

How to add Spring jars to Java Project mirroring folder in git repo

Long title, but pretty much explains it. I've pulled a git repo (spring rest tutorial http://spring.io/guides/tutorials/rest/) which contains multiple standalone java projects each in separate folders. Each of these projects contains gradle build files as well as gradle itself (no need to install gradle), AND the necessary Spring jars (likely in the gradle-wrapper jar). I've created a java project in eclipse that mirrors one of the folders and the changes in eclipse are picked up no problem and it builds fine.
The problem I have is that the Spring dependencies aren't on the Eclipse build path, so I can't use all of the nice Eclipse features (function completion, auto imports, etc.). There's only one jar in each separate folder from the git repo and it's called gradle-wrapper.jar. My guess is that this contains all the Spring dependencies, as the project builds fine, assuming I've typed everything correctly and manually added correct import statements.
Is there a way to set this up so that I'm not getting all these ugly errors in the java project? I attempted to add the gradle jar to the java project build path, but this had no effect. I suppose one option would be to add the spring dependencies separately, but then the Spring on the build path wouldn't necessarily match the Spring dependencies used by gradle for the actual build.
Gradle projects don't package all the dependencies in the repository; one of the primary reasons to use Gradle or Maven is that they'll handle dependencies for you. You need the Gradle Eclipse plugin and to Import->Existing Gradle projects.

How do I add a java gradle submodule to my Android module?

Suppose I have an Android application project in IntelliJ Idea with Gradle. Call it MyApplication.
What I want to do is to add a plain java library module (not Android library project) as a submodule to MyApplication. Lets call this module a testlib.
And then when whole project is built, I want this module compiled to jar and included to /libs folder of MyApplication.
What I tried: I've created a submodule testlib and included it as a dependency to MyApplication, but I get a following warning:
Warning:Gradle: module 'testlib' won't be compiled. Unfortunately you
can't have non-Gradle Java module and Android-Gradle module in one
project.
Is this even possible?
If you've created your project in IntelliJ as an Android-gradle project, then when you go to the "Add new module" window you should see an "Gradle: Java library" option like this:
IntelliJ IDEA does not support Gradle and Non-Gradle modules in one single project. A comment from JetBrains in their forum states this clearly.
The solution is to either convert the Maven modules into Gradle
modules or split the project in two. With the latter approach, if you
build the plain java library module with Maven before building the
Gradle project, you will still be able to use it as a dependency in
the Android application project.
Beware of using different Java language source levels in multi-module
Gradle projects. IntelliJ IDEA does not handle them correctly due to a
bug in either IntelliJ IDEA or Gradle. (There is a pending discussion
whether this should be fixed in the IDE or Gradle.)

Categories