问题
Is there anywhere a tutorial on how to create Java project with Gradle and then build the jar file?
I'm running into various problems with that. When I create Java project and then add Gradle with File -> New -> Module -> Gradle -> ... then I get some errors about Java EE websocket's not available (I'm using Ultimate Edition) but nevermind about that, I managed to create that project by selecting File -> New -> Project... -> Gradle -> ... and now I have my Java project with Gradle working properly (I can debug it in IDE) but when I try to create an artifact (jar file) I get various errors depending on how much I messed up in project structure settings...
Buildfile: build.xml does not exist!
Build failed
or
Error: Could not find or load main class Main
I think that this project is such a mess right now that the best solution would be to create another new project and just copy Main.class and Gradle's dependencies into this new project.
But how to make it properly?
回答1:
- Create new Gradle (not Java) project. Be sure to select Java option in the dialog. Click Next.
- fill GroupId, ArtifactId and version
- Choose your gradle distribution. You can safely use the reccommended option.
- Choose project name and location
- Click finish
- You put your java classes to src/main/java folder. When using third party libraries, make sure you declare them as Gradle dependencies.
- You build your sources using Gradle tab → tasks → build (double click)
- You can see the result of the build in the Run tab
- In case of error, you can see details by clicking the following button:
回答2:
Just encase 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.
回答3:
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.
回答4:
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
来源:https://stackoverflow.com/questions/37100082/java-project-with-gradle-and-building-jar-file-in-intellij-idea-how-to