IntelliJ IDEA Gradle + Groovy - java

I am new to IDEA.
What I would like to do is have a project (or module?) that has maven folder structure (aka src/main/java, src/main/groovy, src/test/groovy, etc), be managed by Gradle and support creation of Groovy classes, their compilation and execution.
What I tried:
Create a "Groovy" Project.
I can add "Maven" support, but not Gradle.
Create a "Gradle" project and add "Groovy" module to it
I can manage dependencies and plugins, but the file structure is screwed up.
The code goes into a sub-folder of the project (aka the name of the module)
I cannot directly add folders to the "src" of the module. When I copy them into the src folder they are considered package names.
What I am looking for:
Solution to the particular problem
General workflow for creating multi-facet (aka Gradle+Groovy or Java+Maven or Web+YouNameIt) project/modules.
Explanation of what is the reason for this paradigm/structure shift in IDEA?

I would suggest not creating the project from inside Intellij.
Since Intellij imports Gradle projects quite nicely you can just import one of the pre-made Groovy Gradle Quick Starts you can find at the root of your Gradle installation.
For example $GRADLE_HOME/samples/groovy/mixedJavaAndGroovy has the exact Java/Groovy layout you describe with the HelloWorld Classes and Tests in place. Just copy it, rename the root folder, import it into Intellij and code!

Solution to the particular problem
0) Turn on Gradle plugin in Preferences -> Plugins
1) Create any Java project (Groovy, Maven, plain Java)
2) Create build.gradle file in base directory
3) Open JetGradle view and click Add. Then select your build file
4) When you do this first time, IDEA will prompt you to locate your local Gradle distribution (you may also change it later in Preferences -> Gradle settings)
As for project structure, Gradle follows Maven conventions, so in the build file you just write:
apply plugin: 'java'
and everything just works.
Create a "Gradle" project and add "Groovy" module to it
I can manage dependencies and plugins, but the file structure is screwed up.
The code goes into a sub-folder of the project (aka the name of the module)
I cannot directly add folders to the "src" of the module. When I copy them into the src folder they are considered package names.
Explanation of what is the reason for this paradigm/structure shift in IDEA?
The main reason is to provide possibility of logical decomposition of your application into separate modules, e.g. app-core, app-web, app-ear etc. Each of this modules produces an artifact: jar, war, ear.
Compare this with other IDEs, say Eclipse, where you would have several different projects (perhaps dependent on each other) to accomplish the same thing. So basically, you may think of IDEA module as of Eclipse project (roughly). Also this greatly simplifies usage of Maven multi-module projects.
As for the src folder: IDEA lets you mark directories inside the module "content root" (base directory of the module) as Source, Test Source or Excluded. If src is marked as Source directory, then obviously everything inside is treated as packages and sources.

I followed the steps in this video:
Create "Gradle" project, with Gradle plugin enabled and Gradle API plugin disabled. Select "Create empty folders" option.
Add "apply plugin: 'groovy'". This will create groovy folders.
Add "" to IML project file in order to be able to create Groovy classes.

Related

Eclipse: how to properly integrate a Maven/Ant project into a larger one?

I have a Java project that depends on a 3-rd party component. This component is available both as a jar and as a Maven/Ant project. One option for me is to simply add the jar as a library. However, I prefer to add the source code into my project since I may need to slightly modify their source code to better suit my needs.
What's the right way to do it in Eclipse?
My main project has a simple structure: src/ and lib/. The external component also has a standard structure: src/, test/, build/, target/, pom.xml, build.xml. So do I need to copy piece by piece (like contents of one src/ into the other src/), in which case what goes where? Or do I somehow copy it all at once? Or smth else?
The best way would be if you use maven on your projet for dependency management. This way, if you have the other projects open in eclipse, your project will resolve them as local projects, but if you don't, maven will try to fetch the jars from the configured nexus repository.
This way, you avoid having to manually configure your Eclipse projects. Maven will be able to configure your project anywhere you want to build it, not having to manually configure dependency resolution.
Import both the projects into eclipse. Add the reference of 3pp jar project to your project as a reference by clicking on Add on build path option. While delivering it as output there will be a dependency to the 3pp jar project. So either deliver it as separate jar and add it to classpath while executing your project else you have to copy the entire source files into your project and deliver it test complete jar.
Making a jar will be handled by eclipse itself.

Creating a jar file in IntelliJ 12.1.4?

I have seen several of this question asked here, but none have fully been answered to my necessity. I have a file I want to create a jar file with so I can simply place it on my desktop and run it. I think I understand the process up to using Process Structure, then I am lost.
Basically, you can generate an artifact in IntelliJ from the build menu -> build artifact.
Dependending on your project, possible artifacts may be found automatically or not (if you use a "build tool" like maven, ant or gradle).
In case of a build tool, check its documentation to find how to generate a jar file from it.
If you're using a pure IntelliJ project, select your project root and hit F4, it will open its settings. In Artifacts, you'll have to add a new jar (the green + at the top of the window). Select "From modules and dependencies...". Then, you'll have to select the module to use and its main class. It should be enough. Then, you'll just have to build the needed artifact. The jar file will be generated in the folder defined in the configuration (Project settings->Project, Project compiler output in case of a single module, otherwise it will be generated in the module's folder)
When you're looking for a feature in IntelliJ, don't forget to use ctrl+shift+A to search it by its name.
Hope it helps

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.

Eclipse - Create Dist directory

Recently I was forced into using Eclipse because of TFS plugins:
I have a few projects that I'm converting to Eclipse projects from Netbeans. Some of these projects reference each other. After starting to convert these projects I quickly found that Eclipse doesn't want to jar projects post-build. So I used an build.xml and created a new 'builder' for each project (whose bright idea was it to not allow me to reuse builders across multiple projects?). After I got all that working I was sitting back thinking about how I would go about building for deployments, and it occured to me that eclipse is not including any of the referenced assemblies in the build output directory. This sucks, because manually creating lib folders and copying over all of the jar files which are required will be error prone, and time consuming. So heres the question. Is there any reasonable way to set up a builder, or property on an eclipse project such that when I build it, it will create a 'dist' directory, containing both the jar'ed project classes, and a lib folder with all of the referenced jars attached to the project?
Is there any reasonable way to set up a builder, or property on an eclipse project such that when I build it, it will create a 'dist' directory, containing both the jar'ed project classes, and a lib folder with all of the referenced jars attached to the project?
Yes, right-click on the Project and select Export. Type "jar" into the search box and select Runnable JAR file. In the export dialog, select the "Copy required libraries..." option. There should also be an option there to save this export as an Ant script.
I dont know how to resolve a list of dependencies using ANT
Next, you should consider using Ivy. This will add dependency management to your build script. There's probably some learning curve here, but these tutorials should help.

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