I'm unable to add custom source set in my gradle project. How to initialize the version numbers in here?
My gradle file looks like:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
sourceSets {
demo
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
You can set custom sourceSets like this in your build.gradle
main {
java {
srcDirs = ['src/gen/java', 'src/main/java']
}
}
The srcDirs is an array, so you can specify more than one source directory.
You can also specify custom source directories for your tests
test {
java {
srcDirs = ['src/integrated-test/java', 'src/test/java']
}
}
You can find some more info here https://www.baeldung.com/gradle-source-sets in section 3. They use the example for integrated tests, but the idea is the same.
Here is the official gradle document for source sets https://docs.gradle.org/current/dsl/org.gradle.api.tasks.SourceSet.html
I found a solution.
If your requirement is projectFolder/src/ -> Here you want to create a custom module other than the main and test. follow the below one.
Its worked in Intellj ide.
sourceSets {
customModuleName.java.srcDir "src/customModuleName"
customModuleName.resources.srcDir "src/customModuleName"
}
// After adding the above block in build file. Go ahead and create a directory, it will show a module name in suggestion
// Then inside module, Go to create a package. Here we can find java and resources folder in suggestion
Related
Let's assume a java project with a project structure like so:
src
itest
java
SourcesTestsItest.java
main
java
gradle_pr
pojo.java
test
java
gradle_pr
SourceSetsTest.java
build.gradle
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.3/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
sourceSets{
itest{
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
java{
srcDirs("src/itest")
}
}
}
configurations{
itestImplementation.extendsFrom(testImplementation)
itestRuntimeOnly.extendsFrom(testRruntimeOnly)
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:28.2-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
implementation('org.apache.httpcomponents:httpclient:4.5.12')
itestImplementation('com.google.guava:guava:29.0-jre')
}
task printSourceSetInformation(){
doLast{
sourceSets.each { srcSet ->
println "["+srcSet.name+"]"
print "-->Source directories: "+srcSet.allJava.srcDirs+"\n"
print "-->Output directories: "+srcSet.output.classesDirs.files+"\n"
print "-->Compile classpath:\n"
srcSet.compileClasspath.files.each {
print " "+it.path+"\n"
}
println ""
}
}
}
task itest(type: Test) {
description = "Run integration tests"
group = "verification"
testClassesDirs = sourceSets.itest.output.classesDirs
classpath = sourceSets.itest.runtimeClasspath
}
Why do I have use the following lines
compileClasspath += sourceSets.main.output
runtimeClasspath += sourceSets.main.output
if the pojo class should be visible in test class under itest as they are in the same package?. How does the default configuration work so test cases can see the generated compiled code of main?
Gradle doesn't know per se how your configurations depend on each other (i.e. it could be that the main configuration depends on itest or the other way around).
For the main and test configurations this dependency is established for you when you apply the java-library plugin.
I am playing a little bit with my own gradle plugin (for gradle 6.5.1). Now I wrote a small test (implemented in java) which is not working:
Project project = ProjectBuilder.builder().build();
ScriptHandler buildscript = project.getBuildscript();
Action<? super MavenArtifactRepository> action = new Action<MavenArtifactRepository>() {
#Override
public void execute(MavenArtifactRepository mavenArtifactRepository) {
mavenArtifactRepository.setUrl("https://plugins.gradle.org/m2/");
}
};
buildscript.getRepositories().maven(action);
buildscript.getDependencies().add("classpath", "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE");
project.getPlugins().apply("java");
project.getPlugins().apply("io.spring.dependency-management");
It says Plugin with id 'io.spring.dependency-management' not found. I thought that I copied the original from https://plugins.gradle.org/plugin/io.spring.dependency-management
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
}
}
apply plugin: "io.spring.dependency-management"
But I guess thats not the case. :-) Any idea how to translate that snippet to java world?
If you want to apply an other plugin in your test you need the other plugin on your classpath. So you have to add to the build file of your gradle-plugin and than its there.
In this case it means write the following to your build.gradle
testImplementation "io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE"
I am converting over to using IntelliJ (version 2019.1). The multi-project directory structure used has the standard src/main/java and src/test/java for each project, but additionally has some non-standard ones such as: src/testsupport/java.
Gradlew (using the internal/recommended gradlew packaged within IntelliJ) is used to import the projects. The Gradle build files include both:
apply plugin: 'idea'
apply plugin: 'java'
Edited to improve clarity
Every project imports fine. Interproject references work to the standard directories. However, when I am in Project B, but need access to src/generated/java or src/testsupport/java from Project A, those are not imported (import statements that compile fine from the gradle command line show up as unresolvable within IntelliJ). Is there a configuration change or something needed to make these take effect?
Currently, I have:
subprojects {
idea {
module {
testSourceDirs += project.sourceSets.generated.java.srcDirs
testSourceDirs += project.sourceSets.testsupport.java.srcDirs
}
}
}
You need help Gradle out by creating a source set for the custom sources your projects define. So from your question, something like:
(using Kotlin DSL)
allprojects {
apply {
plugin("idea")
plugin("java-library")
}
repositories {
mavenCentral()
}
configure<SourceSetContainer> {
create("generated") {
compileClasspath += project.the<SourceSetContainer>()["main"].output
runtimeClasspath += project.the<SourceSetContainer>()["main"].output
}
create("testsupport") {
compileClasspath += project.the<SourceSetContainer>()["main"].output
runtimeClasspath += project.the<SourceSetContainer>()["main"].output
}
}
val api by configurations
val testImplementation by configurations
val testRuntimeOnly by configurations
dependencies {
api(platform("org.junit:junit-bom:5.5.1"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
val test by tasks.getting(Test::class) {
useJUnitPlatform()
}
}
The above will give you:
So now you want to use projectA in projectB, so projectB's Gradle file would include a dependency on projectA:
dependencies {
implementation(":projectA")
}
This should hopefully get you started. Keep in mind, the examples given above use the Kotlin DSL which you should be able to convert back to Groovy.
References:
https://docs.gradle.org/current/userguide/java_plugin.html#source_sets
https://docs.gradle.org/current/userguide/java_testing.html#sec:configuring_java_integration_tests
I hava an example java project package
package com.example.testing;
with such file tree
app
|
src->com->example->testing->Main.java
and a gradle script:
apply plugin: 'java'
apply plugin: 'application'
sourceSets {
main {
java {
srcDirs 'src'
}
}
}
sourceSets.main.output.classesDir = file("classes")
mainClassName = 'com.example.testing.Main'
defaultTasks 'compileJava', 'run'
Now I want to add some module to this project and my folders will be something like this
app
|
src1->com->example->testing->Main.java
src2->com->another_example->another_testing->Library.java
How do I add new source code to gradle script?
I agree with #JB Nizet about respecting standard conventions. If you still insist on being an Anarchist though:
You already have src declared in your sourceset, why not add src1 and src2 as well? You can add them to the same sourceset, or define a sourceset per module if you want.
sourceSets {
main {
java {
srcDirs 'src'
srcDirs 'src1'
srcDirs 'src2'
}
}
}
To reference files outside the project, see this answer.
The question is about "Adding"; the question of the text is describing a more concrete scenario. If one just wants to add an existing directory, this is the way to add:
sourceSets.main.java.srcDirs += ['src/gen/java']
An example full build.gradle is as follows:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
implementation 'com.squareup:javapoet:1.12.1'
}
sourceSets.main.java.srcDirs += ['src/gen/java']
JavaPoet s a Java API for generating .java source files. It is just used as example library for the build.gradle file.
I have a slightly different approach with a Gradle 4.6:
sourceSets {
main {
java {
srcDir 'src/main/java'
srcDir 'build/swagger-code-dummy/src/main/java'
}
}
}
as you can see, I had to specify the directories with the "/main/java" subdirectories as well, otherwise gradle/intellij was not setting the right path.
Maybe this helps someone else too :)
A slightly different solution:
sourceSets.main.java.srcDirs = ['build/jasper', 'src/main/java']
I'm new to gradle and I'm trying to configure gradle with lwjgl3. Because I didn't found a repo where lwjgl3 is hosted i decided that everybody who use this project has to define the path to the lwjgl lib. I created a user.gradle file with contains the paths to the jar and to the natives.
My build.gradle looks like this at the moment.
apply plugin: 'java'
apply from: 'user.gradle'
apply plugin: 'application'
sourceCompatibility = 1.8
targetCompatibility = 1.8
mainClassName = "mp.Main"
println("LWJGL jar path is configured to: ${config.lwjgl3Jar}")
println("LWJGL natives path is configured to: ${config.lwjgl3Natives}")
repositories {
mavenCentral()
flatDir {
dir config.lwjgl3Jar
}
}
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
compile 'net.java.dev.jna:jna:4.1.0'
testCompile 'junit:junit:4.+'
testCompile 'com.carrotsearch:junit-benchmarks:0.7.2'
compile name: 'lwjgl'
}
tasks.withType(Test) {
scanForTestClasses = false
include "**/*Test.class" // whatever Ant pattern matches your test class files
}
sourceSets{
main {
java {
srcDir 'src'
exclude 'mp/graphics/gl/scene/Mesh.java'
exclude 'test'
}
}
test{
java {
srcDir 'src/test'
exclude '**/UnsafeTest.java'
exclude '**/DispatchTests/*'
exclude '**/MemoryTest.java'
exclude '**/SuperFastListTest.java'
exclude '**/MatrixTest.java'
exclude '**/SimulationTest.java'
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
How to set the natives? I tried it different ways. Google didn't helped me out this time. All results are related to older versions of this lib and all are using repositories. Maybe I'm missing the forest for the trees in between. Any ideas?
Best regards!
PS: Not sure if it is important: We are using different IDE's like intelliJ and Eclipse on Windows, Linux, and Mac.
I have run into the same problem and wrote a plugin for handling the natives associated with Java jar files.
http://cjstehno.github.io/gradle-natives/
It will unpack them from the jar files so that you can use them and deploy them in your project.
I solved the problem for me. The problem for was that I didn't knew how to configure gradle to use the natives. Normally I set the the classpath in the run config. However:
The very simple solution how to set the classpath with gradle:
Apply the java plugin and use the function:
run {
systemProperty 'java.library.path', 'path to ur natives')
}
The simply run your application via gradle and it should work.
There were so many solutions by searching for "lwjgl gradle natives" that I didn't found the right one :-)
Hope the solution helps somebody.