cant have project structure "src/main/java" in eclipse using Gradle - java

I used to have a "src/main/java" gradle project structure in eclipse, but after some changes by another person and subsequent pulls from github I lost that project structure and it is now a total mess. Also gradle is not loading any dependency and says "the import ..cannot be resolved"!
How to take control of that?
In the pic I show two diff projects, in the left one with a correct folder structure "src/main/java", in the right the one with issues...pic here
I already tried changes in the build path and it doesn't seem to work.

had the other person updated any information with build.gradle ?
sourceSets {
main {
java { srcDirs = ["src/main/java"] }
resources { srcDir "src/main/resources" }
}
}
I believe this should fix the issue for you , or if you can share your build.gradle file then We can have a look .

Related

How can you add dependency source code to sourceJar task output?

If you got a multi-project gradle build. And one module depends on another.
How could you add the dependency module source code to the output jar
Now i am using this:
java {
withSourcesJar()
}
I am new to gradle builds and i don't know any kotlin.
And if you have the source code of a dependency as a .jar file. Could you also add that
to the output?
So I have a project module:
dependencies:
project module
local .jar
What i want:
One .jar of the project (including other modules and dependencies) compiled code:
project-0.5.0.jar
..and one .jar of the source code (including other modules and dependencies)
project-0.5.0-sources.jar
I have all source code of dependencies stored locally as .jar files
Edit
My project conventions (global for all modules):
plugins {
`java-library`
}
java {
withSourcesJar()
}
How I am currently creating the project "fat".jar with compiled code:
(inside the build script)
tasks.jar {
//manifest.attributes["Main-Class"] = "com.example.MyMainClass"
val dependencies = configurations
.runtimeClasspath
.get()
.map(::zipTree) // OR .map { zipTree(it) }
from(dependencies)
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
I have figured out how to add a project moduleA to another moduleB output sources .jar like so (inside moduleB's build-script):
tasks.sourcesJar {
from(project(":moduleA").sourceSets.main.get().allSource)
}
Now I need to figure out how to include source code from a dependency .jar
from(file("../path/dependency-1.0.0-sources.jar"))
This packs the .jar as it is. I need it's files.
I figured it out. And it was easier than i thought. Keep in mind i am using Kotlin.
(All code snippets are inside the build.gradle.kts file of the project / module you are creating the sources .jar for)
First off you need to include either the java or java-library plugin:
plugins {
`java-library`
}
And as far as i know, also this plugin extension:
java {
withSourcesJar()
}
This makes the sourcesJar task available (task used to create the sources jar), and you can modify it like so:
tasks.sourcesJar {
from(project(":common").sourceSets.main.get().allSource)
from(zipTree("../libs/tinylog-2.5.0/tinylog-api-2.5.0-sources.jar"))
}
The first line inside the brackets includes my "common" module source code to the output .jar.
The second line adds the .java files inside the tinylog sources .jar to the output .jar.

IntelliJ IDEA annotation processing doesn't generate sources in configured forlder

I'm struggling with IntelliJ Idea (IntelliJ IDEA 2018.3.2 (Ultimate Edition)), Gradle, and Immutables library. What I'm trying to do is generating sources in the generated directory as expected by the configuration at Using annotation processor in IDE > IntelliJ IDEA.
At the moment the result I get is that both compiled classes and sources are put inside /build/classes/java/main Have you got the same issues? Do you have suggestions to solve the problem? I'm looking for answers but I didn't find a working solution yet.
Yes, by default Gradle puts all generated sources together with compiled ones.
Please configure it like this:
def generatedJavaSourcesPath = "$buildDir/generated-sources/java"
def generatedJavaSourcesDir = file(generatedJavaSourcesPath)
compileJava {
options.compilerArgs = [
// Override the directory where to place generated source files.
"-s",
generatedJavaSourcesPath
]
}
And to add generated sources to the project
sourceSets {
main {
java {
srcDir generatedJavaSourcesDir
}
}
}
Just add it to the build.gradle

Syncronize Gradle Projects with workspace failed error in eclipse

my application is in java and the IDE used is Eclipse.
I am facing below mentioned issue on refreshing my gradle project.
[Cannot nest
'MyApp-Service/src/test/java/com/mycompany/mymodule/myservice/functional' inside 'MyApp-Service/src/test/java'. To enable the nesting exclude 'com/' from 'MyApp-Service/src/test/java']
here is my folder structure
main folder structure
MyApp-Service/src/main/java/com/mycompany/mymodule/myservice/controller
test folder structure
MyApp-Service/src/test/java/com/mycompany/mymodule/myservice/functional
Below is my build.gradle code snipet
sourceSets {
functionalTest {
java {
srcDir file('src/test/java/com/mycompany/mymodule/myservice/functional')
}
}
}
Most confusing part is if i set the funtionalTest gradle source dir to any other path even if give some junk path the gradle builds fine.
For eg
sourceSets {
functionalTest {
java {
srcDir file('NotAvalidPath')
}
}
}
Please help me to resolve the issue . Thanks in advance.

How to add source files to android studio project?

I currently try to get a huge Android Studio Project to compile. My problem is that I depend on java classes in a different git repo. I fixed it by adding the repo as a submodule. I'm an absolute newbie to gradle, so my approach was to add root, where the .java files are located as a a sourceset:
sourceSets {
main.java.srcDirs += '../../ROOT-OF-SOURCE-FILES/'
}
This resolves the dependencies. In "ROOT-OF-SOURCE-FILES" are a bunch of files not needed for my project and this also causes gradle not to build, because there are also no android files.
Next thing I tried is to point to the specific folders I depend on, but then it say "File path does not correspond to package name"
My question is how can I add the classes (including their dependencies) I need or how can I exclude the stuff I don't need (I also tried the exclude with gradle, but that didn't work).
You can use exclude to ignore parts of your source tree, see https://discuss.gradle.org/t/how-can-i-exclude-certain-java-files-from-being-compiled/5287:
sourceSets {
main {
java {
srcDirs 'main/src'
exclude '**/package2/**'
}
}
}
Another option is to create a symbolic link to the one branch that you really need for Android Studio, e.g.
src/main/
package1
package3
package2
linked/package1 -> src/main/package1
and
sourceSets {
main {
java {
srcDirs 'linked'
}
}
}

Choose Directory for Gradle Generated Source Code

I'm using Dagger 2 to generate some source code in my Gradle project. Right now those sources are being generated and added in the ./build/classes/main folder along with all the class files.
How do I choose a folder to separate all the generated .java files to?
How do I include that folder in my gradle Java project, and have IntelliJ view those as sources so I can use them in my project?
It looks like the application plugin only uses a certain set of directories by default, mixing in flavours of build to decide what files to compile.
However, I did find an example build script that creates a dagger configuration and manipulates gradle into using it for the generated output and adds it to the classpath. It uses dagger-compiler.
The core of it is:
sourceSets {
dagger {
java {
srcDirs = ['src/dagger/java']
}
}
}
configurations {
compileDagger
}
compileJava {
description = "dagger annotation processor is loaded automatically from classpath"
sourceSets.dagger.java.srcDirs*.mkdirs()
classpath += configurations.compileDagger
options.compilerArgs += [
'-s', sourceSets.dagger.java.srcDirs.iterator().next()
]
}
clean {
description = "delete files in generated source directory tree"
delete fileTree(dir: sourceSets.dagger.java.srcDirs.iterator().next())
}
dependencies {
ext.daggerVersion = "2.0.1"
compile(
"com.google.dagger:dagger:${daggerVersion}",
"com.google.guava:guava:18.0")
compileDagger(
"com.google.dagger:dagger-compiler:${daggerVersion}")
}
Regarding IntelliJ, the plugin should automatically add any srcSets via the normal building of the idea project, so there should be no additional configuration needed, just regenerate it.

Categories

Resources