Reputation: 3092
Is there a tutorial explaining how to properly create a Java Gradle project and build the .jar file?
When I create a Java project and add Gradle: File -> New -> Module -> Gradle -> ... I receive errors about Java EE websocket's not available (I'm using Ultimate Edition). However, I can successfully create a project by selecting File -> New -> Project-> Gradle -> which gives me a Java project with Gradle that I can debug. However, when I try to create an artifact (.jar file) I receive errors. I assume the errors stem from mistakes I made in the project structure settings.
Buildfile: build.xml does not exist!
Build failed
or
Error: Could not find or load main class Main
My project is such a mess at this point, maybe I should create another project, then copy/paste the Main.class and Gradle's dependencies from the old project onto the new project.
If this is my best option, how do I correctly create the project this time?
Upvotes: 22
Views: 46965
Reputation: 3547
Just incase you run into no main manifest attribute
problem where the executable Jar
file is not running, I do the following.
1: go to your
build.gradle
and add the following at the bottom.
jar {
manifest {
attributes 'Main-Class': 'your_package_name.YOUR_MAIN_CLASS'
}
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}
- go to
View
>Tool Windows
>Gradle
- A panel at the right side will be shown, now click the
build
for more task. Under thebuild
task double click thebuild
option and wait until the task is done.
- Now, go to your project directory and open the
build
>libs
and the executableJar
file will be there.
I'm not sure if this is the right way.
No need to accept if it works, hope this help the others.
Upvotes: 14
Reputation: 1464
Step 1: Add these lines on your build.gradle
jar {
from {
configurations.runtime.collect {
it.isDirectory() ? it : zipTree(it)
}
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
manifest {
attributes 'Main-Class': 'YOUR MAIN CLASS'
}
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
Step 2: clean
and build
.
I'm sure it will work , Regards
Upvotes: 1
Reputation: 825
If you are using Intellij you can just open the Gradle plugin (it is on the right side of your IDE) and execute a command: bootRepackage. With this you will have a jar in: your_project_folder/build/libs.
Upvotes: 2
Reputation: 17095
Upvotes: 32