Intellij gradle package name incorrect error - gradle

I have just opened this (https://github.com/codetojoy/easter_eggs_for_gradle/tree/master/egg_StackOverflow_51685286) project in IntelliJ with the following files:
src/net/codetojoy/Foo.java
src/net/codetojoy/service/FooService.java
src/net/codetojoy/tests/FooServiceTestCase.java
and build.gradle contains a configuration for sourceSets like so:
sourceSets {
main {
java {
srcDir 'src'
exclude 'net/codetojoy/tests/*'
}
}
}
sourceSets.test.java.srcDir 'src/net/codetojoy/tests'
and On the FooServiceTestCase.java file, I am getting an error on the package line saying Package name 'net.codetojoy.tests' does not correspond to the file path.
I think it is because of the customised source and test set. But I am unsure how to fix it....
please help

The test source set definition needs to look more like the main one:
sourceSets {
main {
java {
srcDir 'src'
exclude 'net/codetojoy/tests/*'
}
}
test {
java {
srcDir 'src'
include 'net/codetojoy/tests/*
}
}
The reason the previous answer worked in command line but not the IDE is that for the Java compiler, the location of a source file is irrelevant. The package instruction is used to correctly place the produced class file, but the source file does not have to be in a matching directory structure. But usually an IDE performs this check.

Related

how to regenerate missing source paths in in a Gradle project?

how to regenerate missing source paths in in a Gradle project? I need to generate all those missing folders like src/java, src/clojure and so on. depends on project plugins. So if I add say Java plugin then src/java/ will be generated.
I just saw that some IDE has an option to create al source paths of the plugins when it imports the project - so I assume it is possible to do from the command line.
You can do that in build.gradle by changing the sourceSets.
sourceSets {
main {
java { srcDirs 'src/java' }
clojure { srcDirs 'src/clojure' }
}
}
If you also need to do the same for the test folders, this should work too:
sourceSets {
test {
java { srcDirs 'test/java' }
clojure { srcDirs 'test/clojure' }
}
}
Java plugin will not create folders for you, it instead assumes a project layout, and you are free to change this structure by doing something similar to what I have above. I have not used the clojure plugin, but I assume it behaves in a similar way.
If you want to list all the folders that the java plugin is making use of, you can do this:
println sourceSets.main.allJava.asPath
Displays all the files being watched by the java plugin.

Using gradle to programmatically add resource folder to test compile/runtime classpath

We have a master build script for 60+ components. The individual components do not have build.gradle files. What I'm trying to do is programmatically (in the master build.gradle) add a resource folder to certain projects. This resource folder contains a file which must be in the classpath when unit tests are ran. I'm trying to add this in the subprojects block like this:
subprojects { proj ->
...
// this is the folder I need in the test task classpath
def resdir = sprintf("%s\\resources", project(':Common').projectDir)
sourceSets {
test {
java {
srcDir 'test'
}
resources {
srcDirs = [resdir]
}
}
}
}
...
if(proj.name == "APROJECT"){
proj.tasks['test'].getClasspath().each {
logger.info "COMPILE CLASSPATH: {}", it
}
}
}
However, if I query the classpath of the test task (see above) I do not see the folder in the classpath. Additionally, of course, the test is failing because the folder is not in the classpath.
If I put the sourceSet update in a build.gradle in the component folder, it works as expected.
Am I doing something wrong? How can I get this folder into the classpath for testing?
I wasn't able to get this to work by dynamically updating the sourceSet, however, I was able to get it to work by adding the necessary resource path to the testCompile dependency. See this for adding a class folder to a dependency.
Update: It's still not an ideal solution since the "solution" only adds the class folder to the compile path, it doesn't treat it as a resource (e.g., copy it to the runtime class folder).
Update #2: It's actually working as expected. It turns out that different tests were referencing slightly different resource paths. Adding all resource paths dynamically as noted above works fine!

How do you add text files to the classpath in Gradle? [duplicate]

How do I add config files or any other resources into my jar using gradle?
My project structure:
src/main/java/com/perseus/.. --- Java packages (source files)
src/main/java/config/*.xml --- Spring config files
Expected jar structure:
com/perseus/.. --- Java packages (class files)
config/*.xml --- Spring config files
I came across this post searching how to add an extra directory for resources. I found a solution that may be useful to someone. Here is my final configuration to get that:
sourceSets {
main {
resources {
srcDirs "src/main/resources", "src/main/configs"
}
}
}
Move the config files from src/main/java to src/main/resources.
Thanks guys, I was migrating an existing project to Gradle and didn't like the idea of changing the project structure that much.
I have figured it out, thought this information could be useful to beginners.
Here is a sample task from my 'build.gradle':
version = '1.0.0'
jar {
baseName = 'analytics'
from('src/main/java') {
include 'config/**/*.xml'
}
manifest {
attributes 'Implementation-Title': 'Analytics Library', 'Implementation-Version': version
}
}
By default any files you add to src/main/resources will be included in the jar.
If you need to change that behavior for whatever reason, you can do so by configuring sourceSets.
This part of the documentation has all the details
I ran into the same problem. I had a PNG file in a Java package and it wasn't exported in the final JAR along with the sources, which caused the app to crash upon start (file not found).
None of the answers above solved my problem but I found the solution on the Gradle forums. I added the following to my build.gradle file :
sourceSets.main.resources.srcDirs = [ "src/" ]
sourceSets.main.resources.includes = [ "**/*.png" ]
It tells Gradle to look for resources in the src folder, and ask it to include only PNG files.
EDIT: Beware that if you're using Eclipse, this will break your run configurations and you'll get a main class not found error when trying to run your program. To fix that, the only solution I've found is to move the image(s) to another directory, res/ for example, and to set it as srcDirs instead of src/.
Be aware that the path under src/main/resources must match the package path of your .class files wishing to access the resource. See my answer here.
As I have answered here, for more granularity while configuring the resource directories it's also possible to use srcDir.
sourceSets {
main {
resources {
srcDir "src/main/resources"
srcDir "src/main"
include "configs/**/*.xml"
}
}
}
So, if you have src/main/java/config/*.xml jar structure will have configs/*.xml as asked.
This is for Kotlin DSL (build.gradle.kts).
Add the following code to your subproject or app build.gradle.kts file:
sourceSets {
main {
resources {
srcDirs("src/main/configs", "src/main/misc")
}
}
// OR another notation
// main.get().resources.srcDirs("src/main/configs", "src/main/misc")
}
As mentioned by other answers, files in src/main/resources/ are automatically added to JAR. The srcDirs() function in above code adds its given paths to that existing path so files in those directories will be included in the JAR as well. You can add as many entries as you want.
Note that after adding the above code and syncing your changes with the IDE, some IDEs like IntelliJ IDEA and Android Studio show a helpful icon for those directories to indicate they are resources root directories:

Gradle: How to exclude an entire directory except one file?

I am trying to exclude all code in package foo.bar except for class MyClass for a certain Gradle subproject.
I tried to add this in build.gradle:
sourceSets {
main {
java {
exclude '**/foo/bar/**'
include '**/foo/bar/MyClass'
}
}
}
But it does not work.
The related question gradle: Exclude all - except one file also does not work, because I am not trying to exclude everything, but only the contents of a certain package.

Filter class files from compiled sourcesets to be added in a JAR in Gradle/Groovy

I'm trying to include the compiled .class files from Project1 into the jar for Project2 since my project structure requires it to be done. For that, in the build.gradle for Project2, I write :
jar {
from project(':Project1').sourceSets.main.output.classesDir
}
Which successfully does what I had to do. But, I now want to filter some of the classes that are added based on path and/or some pattern. For example, to include only delegate files, I tried this :
jar {
from project(':Project1').sourceSets.main.output.classesDir {
include '**/*Delegate*.class'
}
}
But unfortunately it doesn't work. Is there a way to achieve this in Gradle/Groovy?
Using Gradle 2.12, the following works for me (this is build.gradle for Project 2):
task myBuild(type: Jar) {
baseName "myjar"
from project(':Project1').sourceSets.main.output.classesDir,
{ include "**/*Delegate*.*" }
}
From the doc for Jar.from, note that it takes 2 arguments (hence, the comma is used).
Thanks Michael
Although I got my answer as well, I was just missing a parantheses. The correct and working code goes something like this :
jar {
from (project(':Project1').sourceSets.main.output.classesDir) {
include '**/*Delegate*.class'
}
}

Resources