I am trying to test setting up a minimal project, building and exporting it as a jar, and using that project as a dependency in another. I am using Spring for this. I have two projects, makeajar and useajar. In makeajar, I set up a simple project with a Java class called Dinner. I want to import the jar for this project as part of the library in useajar, and from useajar instantiate a Dinner object.
However, the issue I'm getting is that I can't import the Dinner class. When I do, the import shows red:
Is there something I'm doing wrong in this process?
Things I've already tried:
Invalidating caches and restarting
Creating a fresh project
Adding makeajar as a module dependency (note: This was already there, and also appeared in the Project Structure -> Libraries)
How I produce the jar file from makeajar:
Go to project root directory and run ./gradlew build
Things I'm noticing:
Although makeajar appears in my module dependencies and libraries, it does not appear in the "External Libraries" folder in the useajar project.
I can gradle refresh fine, and if I comment out the attempts to import the Dinner object, I build.
I can easily get various popular dependencies (i.e. Jackson, guava) from MavenCentral and use these right out of the box. The issue is only occurring with my makeajar dependency.
makeajar build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.12.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
group = 'com.makingajar'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
useajar build.gradle:
plugins {
id 'org.springframework.boot' version '2.2.12.BUILD-SNAPSHOT'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
id 'idea'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
apply plugin: 'idea'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
mavenLocal {
flatDir {
dirs projectDir.toString() + '/libs'
}
}
}
allprojects {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
implementation 'com.makingajar:makeajar2:0.0.1-SNAPSHOT'
implementation 'com.fasterxml.jackson.core:jackson-core:2.11.3'
}
}
test {
useJUnitPlatform()
}
IntelliJ version 2020.1.4
Gradle version 6.6.1
Related
I am trying to build simple hello world spring-boot project with gradle and groovy.
When I run 'gradle clean build', I am getting below error.
So far I tried,
Even though it is a gradle project, I updated the maven version to latest(3.6.3). Just to be a safer side.
I re-installed my intelliJ IDE
I added maven url as "https:" in build.gradle
After above steps, still I am getting below error. I am not sure what else I am missing.
Though I have given 'https:' url in gradle build file, I am not sure from where it is taking the non-secure url(http).
Task :compileJava FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':compileJava'.
> Could not resolve all dependencies for configuration ':detachedConfiguration1'.
> Could not resolve org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE.
Required by:
project :
> Could not resolve org.springframework.boot:spring-boot-dependencies:2.1.1.RELEASE.
> Could not get resource 'http://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/2.1.1.RELEASE/spring-boot-dependencies-2.1.1.RELEASE.pom'.
> Could not HEAD 'http://repo1.maven.org/maven2/org/springframework/boot/spring-boot-dependencies/2.1.1.RELEASE/spring-boot-dependencies-2.1.1.RELEASE.pom'. Received status code 501 from server: HTTPS Required
And my gradle build file looks:
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
id 'war'
}
group = 'com.own.test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
mavenLocal()
maven {
url = 'https://repo.maven.apache.org/maven2'
}
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Finally, I figured out where to update the maven url to "https://*" for gradle projects.
$ vi ~/.gradle/init.gradle
then I updated the maven url to secure protocol ("https://...")
allprojects {
repositories {
mavenLocal()
// Any Organization/Company specific repo url goes here
maven {
url "https://repo1.maven.org/maven2"
}
}
}
After above steps, it worked for me and able to build the spring-boot application successfully.
Using start.spring.io, this is the content of the build.gradle generated there:
plugins {
id 'org.springframework.boot' version '2.3.1.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'groovy'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.codehaus.groovy:groovy'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
test {
useJUnitPlatform()
}
Note the major differences:
groovy is actually there as plugin and as dependency
the maven repo is secure. As the error message states, you have use
the https endpoint here
I'm new to Gradle and I'm just getting started with it using a multi-project build. Currently, I'm using spring-boot for services and i have the following directory structure
|- api-web (module)
|-- build.gradle
|- api-admin (module)
|-- build.gradle
|- build.gradle (root)
|- settings.gradle
Below are the configurations of the root
build.gradle (root)
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
description = "Company Multi-Project"
ext {
springBootVersion = '2.2.2.RELEASE'
}
bootJar {
enabled = false
}
allprojects {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
repositories {
mavenCentral()
}
}
subprojects {
group = 'com.company'
version = '0.0.1-SNAPSHOT'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
jar {
enabled = true
}
bootJar {
enabled = false
}
dependencies {
// Lombok annotation processor
annotationProcessor 'org.projectlombok:lombok'
compileOnly 'org.projectlombok:lombok'
}
}
settings.gradle (root)
rootProject.name = 'core'
include 'common'
include 'api-web'
include 'api-admin'
Gradle configurations of the modules
api-admin (module)
jar {
manifest {
attributes 'Main-Class': 'com.company.apiadmin.ApiAdminApplication'
}
}
dependencies {
implementation project(':common')
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
I have added jar closure to prevent error on runtime. This avoids the issue where it fails to run saying "No Manifest"
However even after adding that jar closure, now there is a new issue
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
Now I know this says class not found, but my question is how does it occur and why and how to overcome this. I have tried most of the suggestions in Medium, StackOverflow and in Blog posts. Nothing helped. Your help will be highly appreciated. Thanks
I found the solution for my self, it was a bit tricky to find but finally, I found it
The main point is that you should definitely add the spring-boot-gradle-plugin and that will resolve all of your NoClassDef errors and No Manifest Found errors. To add this you should define a build script in your root build.gradle file
buildscript {
ext {
springBootVersion = "2.2.2.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
}
}
Then you should define the plugins for your project
plugins {
id 'java'
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
}
then you can define all the subprojects and allprojects as you normally would.
Then in the modules, in every sub-module just add these lines
apply plugin: 'org.springframework.boot'
bootJar {
launchScript() // optional
}
dependencies {
// your module-specific dependencies
}
and the dependencies as you normally would.
That's it, now you can build using gradle and execute the .jar files as you normally would. :-)
When attempting to use outside dependencies within a custom gradle plugin I'm building, I am not able to import or use them in any regard.
I've attempted to specify in both the build script and the normal dependencies closure my dependencies. I'm using Gradle 5.5 (wrapper script) and I am using the buildSrc method of writing a custom gradle plugin.
// Necessary if loading custom plugins
apply plugin: 'java-gradle-plugin'
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.google.code.gson:gson:2.8.5'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
group 'com.foo'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
gradlePlugin {
plugins {
greeterPlugin {
id = 'com.foo.dbcreation-plugin'
implementationClass = 'com.foo.dbcreation.DbCreation'
}
}
}
dependencies {
compile 'com.google.code.gson:gson:2.8.5'
}
There are quite a few issues I see here.
buildscript does not control the dependencies for your plugin implementation.
Use the plugins {} DSL block to apply plugins. It is the preferred way: https://docs.gradle.org/current/userguide/plugins.html#sec:plugins_block
Should be using implementation over compile since compile is deprecated as noted in https://docs.gradle.org/current/userguide/java_plugin.html#tab:configurations
With that said, your Gradle file should be like:
plugins {
id 'java-gradle-plugin'
id 'eclipse'
id 'idea'
}
group 'com.foo'
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
gradlePlugin {
plugins {
greeterPlugin {
id = 'com.foo.dbcreation-plugin'
implementationClass = 'com.foo.dbcreation.DbCreation'
}
}
}
dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
}
I figured out what my issue was. For projects being built using the buildSrc directory, you need to have the build.gradle file reside in that directory instead of the root project directory (where the build.gradle normally lives). I just converted the project to a normal gradle project and it works just fine.
I was trying to make a jar with dependencies because I was getting a NoClassDefFoundError when starting the jar with java -Dspring.config.location=myProperties -jar myJar, after a lot of searching I found that I can achieve this using the following solution in the jar block:
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
And all good with this except for the amount of time when building the jar (1 minute aprox), and according with this answer: Gradle: Build 'fat jar' with Spring Boot Dependencies I don't need to create an additional task, is enough with the bootRepackage but I'm getting the error that I mentioned above with bootRepackage and I don't understand why.
This is the content of my build.gradle and I'm using spring boot 1.5.15:
/*
* This file was generated by the Gradle 'init' task.
*/
buildscript {
ext.springBootVersion = '1.5.15.RELEASE'
ext.managementVersion = '1.0.6.RELEASE'
ext.axis2Version = '1.7.9'
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
//classpath "io.spring.gradle:dependency-management-plugin:${managementVersion}"
//classpath "com.intershop.gradle.wsdl:wsdl-gradle-plugin:2.0.1"
}
}
plugins {
id 'java'
id 'maven'
id 'org.springframework.boot' version '1.5.15.RELEASE'
id 'io.spring.dependency-management' version '1.0.6.RELEASE'
}
configurations{
implementation{
exclude group: 'javax.servlet', module: 'servlet-api'
}
}
dependencies {
implementation "org.springframework.boot:spring-boot-starter:${springBootVersion}"
implementation 'org.springframework.integration:spring-integration-mongodb'
implementation 'org.springframework.integration:spring-integration-amqp'
testImplementation "org.springframework.boot:spring-boot-starter-test:${springBootVersion}"
testImplementation 'org.mockito:mockito-core:2.6.1'
implementation 'org.apache.ws.commons.axiom:axiom-jaxb:1.2.20'
implementation('org.apache.axis2:axis2-kernel:1.7.9'){
exclude group: 'javax.servlet', module: 'servlet-api'
//The exclude above don't work
}
implementation "org.apache.axis2:axis2-kernel:${axis2Version}"
implementation "org.apache.axis2:axis2-wsdl2code-maven-plugin:${axis2Version}"
implementation "org.apache.axis2:axis2-transport-http:${axis2Version}"
}
wsdl {
axis2 {
genNameAxis2 {
//someAxis2Tasks
}
}
}
wsdl2java {
//someWsdlTasks
}
wsdl2javaExt {
cxfVersion = "3.2.1"
}
jar {
manifest{
attributes ('Main-Class': 'dummy.Application')
}
from{
configurations.runtimeClasspath.collect {it.isDirectory() ? it : zipTree(it)}
}
archiveBaseName = 'projectName'
archiveVersion = '1.0.0'
}
bootRepackage{
mainClass = 'dummy.Application'
//classifier = 'boot' I'm getting an error with this argument
}
repositories {
mavenLocal()
}
group = 'dummy.group'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
Thank you in advance.
I have this same problem after upgrading to Gradle 5 and using 'implementation' instead of 'compile' for my dependencies.
Gradle built a main jar with no sub-project jars or dependencies (no BOOT-INF/lib directory at all). Changing 'implementation' back to 'compile' in the parent project only fixed the problem (with no other changes).
So, apparently, the Spring Boot 1.5.9 Gradle plugin does not work with the new implementation configuration. Note that Spring Boot 2 and the new bootJar task work fine, this issue is only with the old bootRepackage and the new implementation configuration.
I am attempting to work through the Spring Framework Restful Web Service creation tutorial(https://spring.io/guides/gs/rest-service/#scratch) using Gradle and IntelliJ. I have followed everything to the letter but being fairly new to Spring, IntelliJ, and Java in general I'm unsure how to go about further debugging my issue.
When I attempt to build my project I receive a few errors stating "Java: package org.springframework.web.bind.annotation does not exist." I'm guessing I'm missing a library reference but am unsure how to check and include it.
buildscript {
ext {
springBootVersion = '1.3.5.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.springframework:spring-web:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
jar {
baseName = 'hello_springtest'
version = '0.0.1-SNAPSHOT'
}
repositories {
mavenCentral()
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
task wrapper(type: Wrapper) {
gradleVersion = '2.3'
}
eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}
Just thought I'd add some additional information. I'm still seeing the errors and am unsure why but my project does report that the build was successful. When I attempt to make the project however that's when I receive the annotation does not exist error.
You have some dependency in your builds script, which seems to me redundant and causes Gradle to look up for additional dependencies.
Just remove this dependency from your buildscript dependencies
classpath("org.springframework:spring-web:${springBootVersion}")
I see no reason to use it within your buildscript.