Gradle build fail: Read timed out and jcenter - java

I created a new Gradle project today in IntelliJ and couldn't build it, every time it failed with this error:
> Could not resolve org.springframework:spring-core:5.3.17.
Required by:
project : > org.springframework.boot:org.springframework.boot.gradle.plugin:2.6.5 > org.springframework.boot:spring-boot-gradle-plugin:2.6.5
> Could not resolve org.springframework:spring-core:5.3.17.
> Could not get resource 'https://plugins.gradle.org/m2/org/springframework/spring-core/5.3.17/spring-core-5.3.17.pom'.
> Could not GET 'https://repo.gradle.org/artifactory/jcenter/org/springframework/spring-core/5.3.17/spring-core-5.3.17.pom'.
> Read timed out
Since the reported URL contains 'jcenter' in it, I'm wondering if the failure it related to JCenter shutdown. I couldn't explain how it happened since I use mavenCentral repository.
This is my very simple build.gradle file:
plugins {
id 'org.springframework.boot' version '2.6.5'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'org.sample'
version = '1.0.0'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}
When I tried to open https://plugins.gradle.org/m2/org/springframework/spring-core/5.3.17/spring-core-5.3.17.pom in browser, HTTP 303 was returned + location = https://repo.gradle.org/artifactory/jcenter/org/springframework/spring-core/5.3.17/spring-core-5.3.17.pom .
Does anyone face the same issue?

Related

How to import maven dependencies from Nexus

I'm learning about Nexus Repository. And tried uploading an artifact to the maven repository on Nexus. How can I import it into my spring boot project? I have uploaded *.jar file via web interface to my repository. Also, I have tried it but it doesn't find my dependencies.
I got this error
> Could not resolve all files for configuration ':compileClasspath'.
> Could not find mygroup.myartifact:0.0.1:.
Here's what I tried
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example.project'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
repositories {
maven {
url 'http://nexusUrl:8081/repository/my-repo'
credentials {
username = 'admin'
password = 'admin123'
}
}
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-cache'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'mygroup.myartifact:0.0.1'
...
}
Can anyone help me, please?
You should use 'mygroup:myartifact:0.0.1' (note the : instead of the .). This assumes that mygroup is the group and myartifact is the artifact name.

IntelliJ: Unable to use class from jar dependency

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

Gradle SpringBoot Project : Received status code 501 from server: HTTPS Required

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

Gradle file contents is gray

I'm working on small spring boot project using gradle and intellij idea.
After I created a project and restarted intellij idea build.gradle file contents became gray.
But despite this it runs well and I don't get any compilation errors.
Intellij Idea Ultimate version 2019.3.1, java 8, gradle 6.0.1, spring boot 2.1.7
The code of build.gradle is below.
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
developmentOnly
runtimeClasspath {
extendsFrom developmentOnly
}
}
repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
mavenCentral()
}
dependencies {
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
I've tried to restart Idea again. It didn't help. Then I've tried to use an older version of gradle but everything remains as before.

Gradle: multi-project build with spring-boot

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. :-)

Categories

Resources