How to setup a Java Gradle plugin project in IntelliJ? - java

I'm having issues setting up a Java Gradle Plugin project for IntelliJ.
Specifically, I can't get the Java to import the required gradle library.
import org.gradle.api.Plugin;
import org.gradle.api.Project;

I found the answer for Groovy and ported it over for Java.
Insure you have gradle downloaded, and the gradle bin directory added to your path.
Create a new directory for your project to exist in. Open up command prompt, and run the following command:
gradle init --type java-library
Then edit the generated build.gradle file and add the following the the dependencies:
compile gradleApi()
Also and the following:
apply plugin: 'idea'
This should result in a build.gradle that looks like:
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'idea'
// In this section you declare where to find the dependencies of your project
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
compile gradleApi()
// The production code uses Guava
compile 'com.google.guava:guava:20.0'
// Use JUnit test framework
testCompile 'junit:junit:4.12'
}
Then back in command prompt, run:
gradlew idea
And open the generated project in IntelliJ
Groovy Source: How to setup a Gradle plugin project in IntelliJ?

Related

Gradle - cannot load a Java Class even when packed within the jar

My gradle build file is
plugins {
// Apply the java plugin to add support for Java
id 'java'
// Apply the application plugin to add support for building a CLI application
id 'application'
}
apply plugin: 'java'
jar {
from configurations.runtime
manifest {
attributes(
'Created-By':'Gmack',
'Main-Class':'myapprunner.App',
'Class-Path':'mydaos-1.0.jar'
)
}
}
allprojects{
repositories {
jcenter()
}
}
subprojects {
version = '1.0'
apply plugin: 'java'
}
dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:27.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
// Compile Project for dependency
compile project(':mydaos')
}
application {
// Define the main class for the application
mainClassName = 'myapprunner.App'
}
When I run the app using java -jar myapprunner.jar
I get a ClassNotFoundException
Caused by: java.lang.ClassNotFoundException: com.mydaos.Library
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 1 more
I can confirm that the jar has been packed. Not sure why this is not picking things up.
Any help would be appreciated.
Thanks,
Plugin java is being applied twice and com.mydaos.Library is likely being pulled in from compile project(':mydaos') (or 'Class-Path':'mydaos-1.0.jar'). Would assume the project does not build or the class path is wrong.
Dependency classes (projects/external jars) aren't packed inside your jar by default.
You are using the application plugin which bundles your classes, your dependencies and an execution script in a zip so you should use that. The plugin also adds a "run" task to your project to run your main class via gradle for development purposes. See the application plugin docs for more info
If you want to pack your dependencies inside your jar (known as an uber jar) see here. I suggest you stop using the application plugin if you do this
'Class-Path':'mydaos-1.0.jar'
This assumes that mydaos-1.0.jar is in the same folder you are running java -jar ... from which is likely not the case

How to build Spring Statemachine with maven

I downloaded Spring Statemachine (ZIP)
I don't have any a pom.xml/maven instruction
In [site] the maven link isn't available https://github.com/spring-projects/spring-framework/wiki/Downloading-Spring-artifacts
for Maven repository information.
How should I build project with maven ?
Its gradle project.. You can make use of gradle to build it.
Gradle to Maven:
Add Maven plugin in the build.gradle file.
build.gradle should look like this:
apply plugin: 'java'
apply plugin: 'maven'
group = 'com.abc.app'
// artifactId is taken by default, from folder name
version = '0.1-SNAPSHOT'
dependencies {
compile 'commons-lang:commons-lang:2.3'
}
Run gradle install in the directory containing build.gradle will do the job.
It will create pom-default.xml in the build/poms subfolder.
Reference links for Gradle Build:
https://guides.gradle.org/creating-new-gradle-builds/
https://www.eclipse.org/community/eclipse_newsletter/2018/february/buildship.php
https://spring.io/guides/gs/gradle/
https://www.jetbrains.com/help/idea/gradle.html

Unable to build gradle project in eclipse?

After cleaning when i try to build gradle i get an error in the console saying:
package org.json does not exist import org.json.JSONObject;
cannot find symbol
symbol : class JSONObject
there are red marks in the java file at all places where jsonobject and json array exists.
I have put the folder web inf/lib that contains all the jar files inside the src/main/webapp directory that i have created.
currently the contents of my build.gradle file are:
/*
* This build file was auto generated by running the Gradle 'init' task
* by 'i2cdev001' at '14/11/18 3:11 PM' with Gradle 2.14.1
*
* This generated file contains a sample Java project to get you started.
* For more details take a look at the Java Quickstart chapter in the Gradle
* user guide available at https://docs.gradle.org/2.14.1/userguide/tutorial_java_projects.html
*/
// Apply the java plugin to add support for Java
apply plugin: 'java'
apply plugin: 'war'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
// In this section you declare the dependencies for your production and test code
dependencies {
// The production code uses the SLF4J logging API at compile time
compile 'org.slf4j:slf4j-api:1.7.21'
// Declare the dependency for your favourite test framework you want to use in your tests.
// TestNG is also supported by the Gradle Test task. Just change the
// testCompile dependency to testCompile 'org.testng:testng:6.8.1' and add
// 'test.useTestNG()' to your build script.
testCompile 'junit:junit:4.12'
}
Note: Also in the project properties i am unable to see any jar files under the Web App Libraries in the java build path tab. I can see only access rules:no rules defined and native library locations:(none)
As your project is gradle project, Adding jar manually wont work.. You have to mention path where you have kept all your jar files in your build.gradle file.
Mention your jar file path in repository under flatDir {}:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
Then you have to add which jar from that folder you have mentioned above (ie libs)
dependencies {
compile 'gson-0.1.0'
}

How do you include the JMonkey test data jar on a Gradle project?

I've followed the instructions to creating a Gradle project using JMonkey but I have been unable to get any of the assets to load as stated in the tutorial:
http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_asset
My build.gradle looks like this:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
maven {
url 'http://updates.jmonkeyengine.org/maven'
}
}
dependencies {
compile 'com.jme3:jme3-core:3.0.+'
compile 'com.jme3:jme3-effects:3.0.+'
compile 'com.jme3:jme3-networking:3.0.+'
compile 'com.jme3:jme3-plugins:3.0.+'
compile 'com.jme3:jme3-jogg:3.0.+'
compile 'com.jme3:jme3-terrain:3.0.+'
compile 'com.jme3:jme3-blender:3.0.+'
compile 'com.jme3:jme3-jbullet:3.0.+'
compile 'com.jme3:jme3-niftygui:3.0.+'
compile 'com.jme3:jme3-desktop:3.0.+'
compile 'com.jme3:jme3-lwjgl:3.0.+'
}
The sample was created from the wiki: http://wiki.jmonkeyengine.org/doku.php/jme3:maven
However the wiki makes no references to assets or how to build them.
After looking through the internet I found that the jar that I'm looking for is the jME3-testdata.jar. According to this conversation: http://hub.jmonkeyengine.org/t/official-maven-repo-for-jme3-0-stable-available-please-test/30571
It was a deliberate decision not to include the test data jar. Because of this, I went ahead and manually downloaded the missing jar and added it to my classpath.

Why do xml configs of gradle dependencies go with "Gradle__" prefix? Intellij IDEA 13

Java Spring project with Gradle 1.9 and vertx. Local gradle distribution.
Some lines of build.gradle
apply plugin 'java'
apply plugin 'groovy'
apply plugin 'idea'
buildscript {
repositories {
jcenter()
}
}
repositories {
mavenCentral()
}
dependencies {
...
compile 'org.springframework:spring-context-support:3.2.5.RELEASE'
compile 'org.springframework:spring-aop:3.2.5.RELEASE'
compile 'org.springframework:spring-aspects:3.2.5.RELEASE'
...
}
I have an existing gradle project downloaded from git with xml configs in .idea/libraries folder named spring-aop_3_2_5_RELEASE.xml, for example, where we can find xml tag <library name="spring-aop-3.2.5.RELEASE">...</library>.
After I had imported this project new file Gradle__spring-aop_3_2_5_RELEASE.xml appeared with only difference in name attribute of the library tag: Gradle: spring-aop-3.2.5.RELEASE. So i have duplicate xml configs for dependencies. I wonder why my gradle added that prefix.
The prefix is hardcoded, IDEA 13 needs a reimport of your old Gradle projects that were created in IDEA 12. It's not obvious, but there will be a notification about it in the next update.
In the Gradle generated project you can exclude the library files from the version control, same for the .iml files that can be also ignored when using Maven. Other files can be still shared (like code style, run configurations, inspection profiles, etc). Check this document for details.

Categories

Resources