I am trying to convert my project to gradle and unable to get the class files generated under build folder. Here is the build.gradle file:
import org.gradle.internal.os.OperatingSystem;
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
sourceSets {
main {
java {
srcDirs = ["abc/def/"]
include('include/Constants.java')
}
}
}
task initial(type: Exec) {
doFirst {
println 'Started compiling constants'
}
if(OperatingSystem.current().isWindows()) {
println 'Compiling on windows'
//Run some bash script here
}
else {
print 'Compiling on unix'
//Run some shell script here
}
doLast {
println 'Finish compiling constants'
}
}
jar {
baseName = 'output'
}
dependencies {
compile files('../lib/log4j/log4j.jar')
compile files('../lib/findbugs/findbugs.jar')
compile files('../lib/guava/guava-19.0.jar')
}
When i do gradle build, it says successfull, but see no classes generated under build folder.
Any help?
Fixed it after some debugging. The issue was the include after the srcDirs did not work as the path was one level higher than the path defined in srcDirs.
I had a similar issue the other day, it was not generating the compiled classes when I run the build on IntelliJ or even on the terminal.
After trying a lot of things what solved the issue was deleting the .gradle directory and running again a ./gradlew clean build.
It was a weird one, but I hope it might help to someone having the same issue.
Related
I'm adding Checker Framework to an existing Java Gradle build. I have Checker integrated and raising issues and that's all working great. But now that I've added it, my build is no longer producing a .jar file as it once did.
Previously, I'd get a .jar at build/libs/myproject.jar. Now instead I see build/checkerframework and build/main but no build/libs and no .jar.
My build.gradle is below. Anyone attempted this before? Any success?
I guess I'd also accept an answer that shows how run Checker outside of the build, e.g. gradle check to run a build with Checker, and gradle build to produce a .jar. But I'd really prefer to have just a single build step if at all possible.
Thanks!
plugins {
id "org.checkerframework" version "0.5.18"
id 'application'
}
apply plugin: 'org.checkerframework'
checkerFramework {
checkers = [
'org.checkerframework.checker.nullness.NullnessChecker',
'org.checkerframework.checker.units.UnitsChecker'
]
}
repositories {
jcenter()
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
// Embed all dependencies to create a "fat jar" as required for AWS deployment.
// Exclude 3rd-party signing files to prevent security errors at runtime
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude 'META-INF/*.RSA', 'META-INF/*.SF', 'META-INF/*.DSA'
}
manifest {
attributes('Main-Class': 'client.CliMain')
}
}
If the build is passing, then adding -processor should not affect whether a .jar file is produced. (Unless you pass -proc:only or the like.)
If the build is failing, then Gradle won't build the jar files.
You said the Checker Framework is "raising issues", so maybe the build is failing.
I am working on a java project using gradle for a homework assignment and I got troubles running JAR files built off gradle via the windows command prompt :
image
Here's my build.gradle txt :
apply plugin: 'java'
apply plugin: 'antlr'
repositories {
mavenCentral()
}
dependencies {
antlr 'org.antlr:antlr4:4.7.1'
testCompile 'junit:junit:4.12'
}
generateGrammarSource {
arguments += ['-no-visitor', '-no-listener']
outputDirectory = new File(buildDir.toString() + "/generated-src/antlr/main/TP2/")
}
jar {
manifest {
attributes (
'Main-Class': 'TP2.Main',
'Class-Path': configurations.runtimeClasspath.files.join(' ')
)
}
}
I've already checked my classpath directory, class name... When I type the next command : jar -tf build\libs\TP2.jar I can see my class well packed in my jar and my class is at this directory : build\classes\java\main\TP2\Main.class
So I don't see why it is not working.
I'm not an expert with gradle that's why any help would be really appreciated but I think the problem propably come from the manifest.
Gradle with apply plugin: 'java' in build.gradle. The file will create a .jar file and the test task is running junit tests:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.12'
}
This is working. But to make sure the public API tests are working with the generated .jar file I want that the 'test' task is running the test with the generated .jar file from the build/libs folder in classpath and not with the generate .class files from folder build/classes in in classpath.
Not working because the sourceSets is set global:
tasks.withType(Test) {
sourceSets {
main {
java {
exclude '**'
}
}
}
}
Partly working: multiproject (test and jar separated in two gradle projects):
dependencies {
compile project(":jar_project")
testCompile 'junit:junit:4.12'
}
in this case jar_project.jar is used but package private test are also executed without an error.
Do somebody have an idea how to run the tests with the .jar as dependency and ignoring the .class files?
Thank you, pulp
The problem is that the test task does not depend on the jar task, which sort of makes sense because the jar is supposed to package the classes, so there should be no reason for tests to depend on that jar.
You can force the dependency by:
test {
dependsOn jar
doFirst {
classpath += jar.outputs.files
}
}
Now the jar will be on the test classpath
I have some code that I do not want included in the jar file based on a condition.
My build script looks like
plugins {
id 'java'
id 'org.springframework.boot' version '2.0.0.RELEASE'
}
sourceSets {
main {
java {
if (project.environment == 'prod') {
exclude '**/dangerous/**'
}
forEach {
println it.absolutePath
}
}
}
}
Now, when I run the script with gradlew clean build bootJar -Penvironment=prod the absolute paths of everything but the dangerous java files is printed, but they are still included in the jar.
If I remove the boot plugin and run the jar task, the dangerous class files are still included in the jar.
gradlew clean build jar -Penvironment=prod
plugins {
id 'java'
}
sourceSets {
main {
java {
if (project.environment == 'prod') {
exclude '**/dangerous/**'
}
forEach {
println it.absolutePath
}
}
}
}
If I add an exclude clause to the jar task, the dangerous files are not printed, and they are not included in the jar.
gradlew clean build jar -Penvironment=prod
plugins {
id 'java'
}
sourceSets {
main {
java {
if (project.environment == 'prod') {
exclude '**/dangerous/**'
}
forEach {
println it.absolutePath
}
}
}
}
jar {
exclude '**/dangerous/**'
}
If I enable the boot plugin, and use the bootJar task (which inherits from the Jar task) (gradlew clean build bootJar -Penvironment=prod), I do not see the dangerous files printed, but the files are still included in the jar.
plugins {
id 'java'
id 'org.springframework.boot' version '2.0.0.RELEASE'
}
sourceSets {
main {
java {
if (project.environment == 'prod') {
exclude '**/dangerous/**'
}
forEach {
println it.absolutePath
}
}
}
}
bootJar {
exclude '**/dangerous/**'
}
How can I exclude a java file conditionally with the Spring Boot Gradle Plugin and bootJar task?
I was having same issue when i was using 2.0.1.RELEASE. I created jar using bootJar option. Add exclude inside it with file patterns which you want to exclude from executable jar.
This worked fine with spring 2.0.4.RELEASE version.
bootJar {
exclude("**/dangerous/*")
}
I narrowed down the problem. I didn't put in all of the plugins up above, because I thought the only important ones were java and spring boot. However, my actual code also uses the protobuf plugin. If I remove the configuration property generatedFilesBaseDir, then it successfully excludes the dangerous directory.
However, this opens up a new question of, what the hell is happening?
I was specifying the generated files base dir property so I could reference the generated classes in my source code, but I think I may need to create a different project just for the proto, and add that project as a reference to my main module.
Edit
Making a separate project for the protobuf files and referencing it as a project seems to be a viable workaround for this issue.
build.gradle:
apply plugin: 'java'
sourceCompatibility = 1.8
targetCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Main-Class': 'app.Main'
}
}
repositories {
mavenCentral()
}
dependencies {
}
Main.java:
package app;
public class Main {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I need to build a jar, so I could run it from command line
java -Xms6000m -Xmx7100m -jar sample.jar
My project is more complex than "Hello World", the problem I am trying to solve is that my jar has only MANIFEST file
Manifest-Version: 1.0
Main-Class: app.Main
I created my project in Eclipse IDE with buildship plugin. (I do not think there is a problem)
I build my project in command line.
gradlew clean build
Command line output
C:\Development\Workspaces\training\sample>gradlew clean build
BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed
C:\Development\Workspaces\training\memoryAllocation2>
My project structure.
Your source files are not matching your build script. You either have to put your files in the conventional src/main/java/ directory, or change your build script to tell Gradle where to find the source files.
To summarize the various comments into one complete answer:
Gradle uses, just like Maven, a approach called convention over configuration. This also includes a convention for the location of source files. Without any configuration, Gradle expects the source files for your application under src/main/java. Test sources can be placed under src/test/java.