Gradle doesn't seem to be resolving my project dependency properly - java

I'm pretty new to Gradle and am having an issue getting my a module that is dependant on another module to build properly.
So I have the following configuration for my modules.
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.10'
}
}
project(':web-service') {
apply plugin: 'war'
dependencies {
compile project(':core')
compile('com.sun.jersey:jersey-server:1.7')
compile('com.googlecode.json-simple:json-simple:1.1.1')
}
}
project(':core') {
dependencies {
compile('log4j:log4j:1.2.17')
}
}
If I try to build my core project everything succeeds as expected.
However, if I try to build the web-service project with the following command:
gradle :web-service:build
It appears to build the core project first as expected but then encounter build errors that indicate that classes that exist in the core module cannot be found.
What gives?

Turns out this was completely my fault. I dug deeper on the error messages that I was getting and found some package does not exist messages at the top. Turns out that my directory structure was not inline with my package names.

Related

How do you parameterize the Spring Boot Gradle plugin?

We are looking to migrate from Maven to Gradle, and have worked through most of the challenges you would expect for replacing the parent POM concept. There is one sticky point that we haven't figured out yet. We need to specify the version of Spring Boot we are using globally, but I run into invalid build file problems with both of the solutions I've tried:
I tried putting the plugins { id 'org.springframework.boot' version '2.1.17.RELEASE' } declaration in the common build script. Build error, "Only Project and Settings build scripts can contain plugins {} blocks."
I tried calling the common build file to specify the springBootVersion parameter and using that in the plugins declaration. Build Error, "only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed"
All of this would be easier if I could simply apply plugin: 'org.springframework.boot' but then Gradle can't find the plugin. All but one microservice are on a single version of Spring Boot, and we want to be able to upgrade globally if possible.
Additional Information
I have ~40 microservices plus some libraries used by those services
Separate repository for each of them, so the normal parent/child approach does not work
Maven parent POMs allowed you to publish that POM as it's own resource, and there is no 1:1 equivalent feature in Gradle
Gradle pluginManagement concept also doesn't work for us because it resolves the Spring Boot plugin but the dependency management plugin now can't be found.
My common build script is included here:
repositories {
mavenLocal()
/* Removed our internal repositories */
jcenter()
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'maven-publish'
apply plugin: 'io.spring.dependency-management'
group = 'nedl-unified-platform'
/* Required to publish Spring Boot microservices to publish to repository */
configurations {
[apiElements, runtimeElements].each {
it.outgoing.artifacts.removeIf { it.buildDependencies.getDependencies(null).contains(jar) }
it.outgoing.artifact(bootJar)
}
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
withJavadocJar()
withSourcesJar()
}
ext {
set('springBootVersion', '2.1.17.RELEASE')
set('springCloudVersion', "Greenwich.SR6")
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
jacoco {
toolVersion = "0.8.5"
reportsDir = file("$buildDir/reports/jacoco")
}
test {
finalizedBy jacocoTestReport // report is always generated after tests run
}
jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.2
}
}
}
}
jacocoTestReport {
dependsOn test // tests are required to run before generating the report
reports {
xml.enabled true
html.destination file("${reportsDir}/jacocoHtml")
xml.destination file("${reportsDir}/jacocoReport.xml")
}
}
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
repositories {
/* excluded for privacy and brevity's sake, our internal Maven repo */
}
}
And that is called by our project build script that I want to parameterize:
plugins {
id 'org.springframework.boot' version springBootVersion
}
apply from: "https://mycentral.repo/project-common/develop/build.gradle"
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
implementation 'ch.qos.logback:logback-classic'
implementation 'javax.annotation:javax.annotation-api:1.3.2'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
implementation 'org.glassfish.jaxb:jaxb-runtime:2.4.0-b180830.0438'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
version = '0.0.2-SNAPSHOT'
I think the gap here is that in maven you have the concept of a parent pom, whereas in Gradle you don't. There is no 1:1 mapping to this like you say, but you can have plugins in Gradle, and apply a plugin.
The closest thing you would have is if you developed your own Gradle plugin, which each of your projects could apply. Your custom plugin would then configure Spring Boot among whatever else is common to all your projects. This plugin would define the version of Spring Boot you want all your other projects to use.
You wouldn't get much benefit to a custom plugin if it's only concern is configuring Spring Boot, it would need to do other things as well. It can be difficult to create a Gradle plugin when you don't have allot of experience in it. You lose all the familiar syntax to the build.gradle and you literally have to write code, (there are some similarities but I have found it difficult), I would avoid it if possible.
I would suggest you start off by applying the spring boot plugin directly to one of your microservices projects, get that working, then do another. After you have done a number of them you will then be able to see what is common between them, and if it is indeed worth investing into developing a global plugin. You really need to be careful though because your global plugin has the potential to be both a blessing and curse. It may take away allot of manual work for maintainers, but if you get it wrong it will cause them grief, and then they will want to go back to maven.
I'm not sure if I understand your globally defined Spring version requirement. Unless you are using SNAPSHOT dependencies/plugins (bad don't do that), (or a black magic settings.gralde outside of your repo), you will have to put some version somewhere. As an alternative you could create your own custom task which runs on the check lifecycle which will check the version of spring (or your plugin) and print a warning if it's not the latest version, and encourage the developer to upgrade.
Extra Info
Parameterizing plugins with properties can be done putting your property in gradle.properties as springBootVersion=2.1.17.RELEASE .
I'm not sure I understood your issue perfectly but you should use the Gradle way for sharing configuration : the root project config.
Instead of including the common build script in every project, create a global project and set the configuration here.
root
|
| --- projectA
| --- projectB
| --- projectC
With the according settings.gradle
include 'projectA'
include 'projectB'
include 'projectC'
In the root build.gradle, set up the version
ext.springBootVersion = '2.1.17.RELEASE'
In subprojects using springBoot, let's say projectB, apply the plugin in the sub build.gradle
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
apply plugin: 'org.springframework.boot'
This example works for me, though I may not understand all of the constraints.
If we abstract the version of Spring Boot behind a fixed URI (e.g. on an internal CI/CD server), then consider this in each project/repo's build.gradle:
buildscript {
repositories {
jcenter()
}
def SPRING_BOOT_VERSION_URI = 'http://localhost:5151/api-server/spring-boot.txt'
ext.springBootVersion = new URL(SPRING_BOOT_VERSION_URI).getText().trim()
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
apply plugin: 'org.springframework.boot'
apply from: "../common/build.gradle"
I realize the original question states that the apply plugin doesn't work, but it's not clear to me if that precludes this method.
Finally, note that it is easy to expand this beyond a simple text-file to be a more formal JSON specification (tailored to the teams' needs).
If you add this to the root project, all child projects should be able to just import from the same set of Spring Boot dependencies. The magic ingredient is the allprojects block:
buildscript {
repositories {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
ext {
springBootVersion = '2.3.4.RELEASE'
}
allprojects {
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
dependencyManagement {
imports {
mavenBom("org.springframework.boot:spring-boot-dependencies:${springBootVersion}")
}
}
}
apply plugin: 'org.springframework.boot'

Gradle building Spring Boot library without Main Class

I am trying to build a Spring Boot/Gradle project and create a jar without a main class. My purpose is that this project is a library that will be pulled in by other projects therefore the library project does not require a main class to run. Unfortunately, no matter what kind of gradle config I write I keep getting errors when I try to build install about not having a main class or not being able to find the bootJar task.
Here's what my gradle file looks like:
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE' apply false
}
apply plugin: 'io.spring.dependency-management'
apply plugin: 'maven'
dependencyManagement {
imports {
mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
}
}
repositories {
mavenCentral()
}
jar {
enabled = true
}
bootJar.dependsOn fooTask
But when I run this I get the following error:
Could not get unknown property 'bootJar' for root project
'foo-library' of type org.gradle.api.Project.
What in my configuration needs to change?
Disable bootJar in your build.gradle
bootJar {
enabled = false
}

Sudden loss of dependencies in Eclipse Gradle project

I'm working with a Gradle project in Eclipse Oxygen with Java 8 and Gradle wrapper 4.6.
When i tried to add a new dependency compile 'com.googlecode.json-simple:json-simple:1.1.1' and refresh the project, all the "Project and External Dependencies" folder disappeared without a trace, which stops my project running from Eclipse (it can run through Gradle Run task though).
Removing the newly added dependency does not save me. Neither gradlew cleanEclipse & gradlew eclipse nor refreshing the project countless times.
I also attempted to clear the cache and forced project to rebuild when reimport, but no "Project and External Dependencies" shows up.
What should i do to bring back the old dependencies?
Here is my build.gradle
// Apply the java plugin to add support for Java
apply plugin: 'java'
// Apply the plugin to be runnable
apply plugin: 'application'
// Apply the eclipse plugin to import project on other machines
apply plugin: 'eclipse'
// JUnit 5
apply plugin: 'org.junit.platform.gradle.plugin'
// Define the entry point
mainClassName = 'meshIneBits.MeshIneBitsMain'
applicationName = 'MeshIneBits'
// In this section you declare where to find the dependencies of your project
repositories {
// Use 'jcenter' for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
sourceCompatibility = 1.8
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'org.processing:core:3.3.5'
compile 'org.jogamp.jogl:jogl-all:2.3.2'
compile 'org.jogamp.gluegen:gluegen-rt:2.3.2'
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-amd64"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-armv6"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-armv6hf"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-linux-i586"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-macosx-universal"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-windows-amd64"
runtime "org.jogamp.gluegen:gluegen-rt:2.3.2:natives-windows-i586"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-amd64"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-armv6"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-armv6hf"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-linux-i586"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-macosx-universal"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-solaris-i586"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-windows-amd64"
runtime "org.jogamp.jogl:jogl-all:2.3.2:natives-windows-i586"
testCompile('org.junit.jupiter:junit-jupiter-api:5.1.0',
'org.junit.jupiter:junit-jupiter-params:5.1.0')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.1.0',
'org.junit.platform:junit-platform-launcher:1.1.0')
}
eclipse {
project.natures 'org.eclipse.buildship.core.gradleprojectnature'
classpath {
file {
defaultOutputDir = file('bin/main')
whenMerged {
//change output folder for tests
def testsrc = entries.find { it.path == 'src/test/java' }
testsrc.output = "bin/test"
def testres = entries.find { it.path == 'src/test/resources' }
testres.output = "bin/test"
// Remove local ref for libs
entries.removeAll{ it.kind == "lib" }
}
containers 'org.eclipse.jdt.junit.JUNIT_CONTAINER/5', 'org.eclipse.buildship.core.gradleclasspathcontainer' }
}
}
buildscript {
repositories { mavenCentral() }
dependencies { classpath 'org.junit.platform:junit-platform-gradle-plugin:1.1.0' }
}
jar {
baseName = applicationName
version = '0.2.0'
manifest { attributes 'Main-Class' : mainClassName }
}
Edit:
After switching to IntelliJ, the problem is solved (i used the same folder of project). So i think this is a problem in Eclipse.
I also had this problem where the "Project and External Dependencies" folder disappeared. It instead showed as 'org.eclipse.buildship.core.gradleclasspathcontainer'.
If you do not see that line, then it needs to be added to the .classpath file.
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer" />
The fix that worked for me was closing Eclipse, and running
gradlew cleanEclipse eclipse
then opening Eclipse and adding the Gradle nature to the project.
I also encountered this problem when I refactor the project name.
Following steps solve my problem:
add eclipse plugin to build.gradle
right click build.gradle, choose gradle then select refresh gradle project
only refresh gradle project didn't help me.

Trying to include Gradle project as dependency, but include() not working

I have a Gradle project which depends on another Gradle project. My project structure is like this:
project1
build.gradle
settings.gradle
src/
project2
build.gradle
settings.gradle
src/
in project1/build.gradle I want to add project2 as a dependency:
version '1.0-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main {
java {
srcDirs = [ 'src' ]
}
}
}
include ':project2'
project(':project2').projectDir = new File("../project2")
dependencies {
compile project(':project2')
}
Unfortunately, I'm always getting this error message:
Error:(21, 0) Could not find method include() for arguments [:project2] on root project 'project1' of type org.gradle.api.Project.
I'm using Gradle 3.5 and I'm getting this error both on the command line (gradle build) and in IntelliJ. I found a few StackOverflow threads about the same issue (this and this), but they were not helpful.
The Gradle multi-project documentation also doesn't mention any specific requirements which I may be missing that can cause the error.
When I leave the include call out, I get the message that the project path could not be found in the root project.
I also tried moving the dependency to a subdirectory of project1, but without success.
I wonder what I'm doing wrong and why apparently not many other people are having the same problem. I'd be grateful for hints.
Note: this is not an Android project.
As pointed out in the first comment, include actually needs to go into settings.gradle and not into build.gradle. The same applies to changing the projectDir property.
Comment 3 gave also me another idea. The project can be included in settings.gradle as follows:
includeBuild '../project2'
and in project1/build.gradle I specify the dependency as
dependencies {
compile 'group:project2:version'
}
I generally like this better, since it's less code and looks cleaner. The downside, however, is that recursive composite builds aren't possible. So if project2 itself is also a composite build, this won't work.

How do you include the JMonkey test data jar on a Gradle project?

I've followed the instructions to creating a Gradle project using JMonkey but I have been unable to get any of the assets to load as stated in the tutorial:
http://wiki.jmonkeyengine.org/doku.php/jme3:beginner:hello_asset
My build.gradle looks like this:
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = 1.8
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
repositories {
mavenCentral()
maven {
url 'http://updates.jmonkeyengine.org/maven'
}
}
dependencies {
compile 'com.jme3:jme3-core:3.0.+'
compile 'com.jme3:jme3-effects:3.0.+'
compile 'com.jme3:jme3-networking:3.0.+'
compile 'com.jme3:jme3-plugins:3.0.+'
compile 'com.jme3:jme3-jogg:3.0.+'
compile 'com.jme3:jme3-terrain:3.0.+'
compile 'com.jme3:jme3-blender:3.0.+'
compile 'com.jme3:jme3-jbullet:3.0.+'
compile 'com.jme3:jme3-niftygui:3.0.+'
compile 'com.jme3:jme3-desktop:3.0.+'
compile 'com.jme3:jme3-lwjgl:3.0.+'
}
The sample was created from the wiki: http://wiki.jmonkeyengine.org/doku.php/jme3:maven
However the wiki makes no references to assets or how to build them.
After looking through the internet I found that the jar that I'm looking for is the jME3-testdata.jar. According to this conversation: http://hub.jmonkeyengine.org/t/official-maven-repo-for-jme3-0-stable-available-please-test/30571
It was a deliberate decision not to include the test data jar. Because of this, I went ahead and manually downloaded the missing jar and added it to my classpath.

Categories

Resources