import failed using gradle dependency on windows 10 using IntelliJ - java

I am following the spring boot documentation https://spring.io/guides/gs/spring-boot/
but after I did the same as in Add Unit Tests step, even though I have the same gradle file, the project will not build because the import failed.
gs-spring-boot\complete\src\main\java\com\example\springboot\HelloControllerTest.java:3: error: package org.hamcrest does not exist
import static org.hamcrest.Matchers.equalTo;
^
error: package org.assertj.core.api does not exist
import static org.assertj.core.api.Assertions.*;
My gradle file content
plugins {
id 'org.springframework.boot' version '2.2.2.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
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'
}
}
test {
useJUnitPlatform()
}
and entire project setup same is given in https://github.com/spring-guides/gs-spring-boot/archive/master.zip
What went wrong here? I have the java SDK setup and I am able to run the http server right before this step. I am on windows 10 using IntelliJ and its built in gradle.

Not sure if this is still a useful answer, but I also ran into this issue.
The problem seems to be that your code is in the 'main' directory instead of the 'test' directory. Moving the test files into the test directory fixed this problem for me.

You're missing Hamcrest:
// https://mvnrepository.com/artifact/org.hamcrest/hamcrest
testImplementation "org.hamcrest:hamcrest:2.2"

Related

Gradle not resolving dependencies of dependencies

Gradle won't build because it can't resolve dependencies of dependencies. For example, I have a project MyStarterWeb which has a dependency on spring-boot-starter-web:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web:3.0.2'
}
I have a second project, MyFirstEndPoint, that extends/uses/is dependent upon MyStarterWeb:
dependencies {
implementation 'group: 'com.mygroup', name: 'MyStarterWeb', version: '0.0.1-SNAPSHOT'
}
After reading the Gradle docs (which are contradicting, confusing and of little value), I understood that Gradle would resolve all the dependencies for the MyFirstEndPoint project without having to list the spring dependencies listed in MyStarterWeb. But, this is not the case:
error: package org.springframework.boot does not exist
import org.springframework.boot.SpringApplication;
Any help/explanation/guidance/clarification on the poor Gradle docs is greatly appreciated. If there's an RTFM link that I missed, please share
For anyone who runs into this issue with Gradle not resolving transitive dependencies (using Gradle 6.0+) for a self-published artifact/library, the solution was to change the MyStarterWeb project to use the java-library plugin:
plugins {
id 'java'
id 'java-library'
id 'org.springframework.boot' version '3.0.2'
id 'io.spring.dependency-management' version '1.1.0'
}
and then to change the dependencies in MyStarterWeb to use the api configuration instead of implementation configuration.
dependencies {
api 'javax.servlet:javax.servlet-api:4.0.1'
api 'org.springframework.boot:spring-boot-starter-web:3.0.2'
}
That allowed the MyFirstEndPoint project to resolve all transitive dependencies and only have to specify the MyStarterWeb dependency
dependencies {
implementation group:'bla', name: 'MyStarterWeb', version: '0.0.1-SNAPSHOT'
}

Running projects without Eclipse

In my company, plenty of teams (including mine) don't know how to run Spring projects without Eclipse to a point that this is becoming a problem. I've extensively searched around for stuff, but stumbling upon places just saying "press Run" for the project to be ran. Many times that doesn't help us understand the dependencies of legacy projects, and of course, legacy projects have no documentation laying around, or the folks who designed it (or the folks who designed it don't know how to run that without the IDE).
Here is the architecture of the latest one:
Spring, Gradle, Java.
I've tried running this project with ./gradlew run, but the task run doesn't seem supposed to run the application, the task doesn't even exist and the build.gradle file doesn't have it.
This is the build.gradle file:
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.name'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-actuator'
compileOnly 'org.projectlombok:lombok:1.18.4'
}
This is the Java file that is suppose to be the runner:
package com.company.authApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class AuthApplication {
public static void main(String[] args) {
SpringApplication.run(AuthApplication.class, args);
}
}
Even if the build.gradle file mentiones mavenCentral(), this is not build for Maven at all, that's just the default structure for it.
Any help is appreciated.

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

no main manifest attribute, in myFile.jar

I have a problem with the creation of my API jar written in spring.
The problem is that every time I try to run my docker image the error is: "no main manifest attribute, in myFile.jar"
The API must be deployed on my RaspBerry pi
I share a little more details ->
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.6.RELEASE'
id 'java'
}
apply plugin: 'java'
apply plugin: 'io.spring.dependency-management'
group = 'me.myddns.findyourhome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '12'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-dependencies:2.0.5.RELEASE'
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
springBoot {
mainClassName = 'me.myddns.findyourhome.ApiDispatcher'
}
(me.myddns.findyourhome.ApiDispatcher is the name of the folder of the of the main class, I try to add .MainFileName, but nothing change)
Dockerfile
FROM hypriot/rpi-java
COPY ApiDispatcher.jar .
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "ApiDispatcher.jar"]
I don't know where the error can come from, so if you need some other details to help me, let me know.
thanks in advance!
SOLVED
The problem was that Intellij in the import of the project had confuse some directories. I solved in reimport the project as a gradle project, like a new import!
Hope I can help some one else in my situation :D

Which is the proper Gradle plugin to support 'provided' method?

I'm currently trying to include Project Lombok helper into my Gradle project, but while following their instructions for Gradle within my build.gradle, I'm getting the following error:
Error:(11, 0) Build script error, unsupported Gradle DSL method found: 'provided()'!
Possible causes could be:
you are using Gradle version where the method is absent
you didn't apply Gradle plugin which provides the method
or there is a mistake in a build script
My current build.gradle file:
apply plugin: 'java'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
provided "org.projectlombok:lombok:1.14.4"
testCompile group: 'junit', name: 'junit', version: '4.11'
}
As of release 2.12, provided scope is called compileOnly
Old answer:
Provided scope is available in 'war' plugin (http://www.gradle.org/docs/current/userguide/war_plugin.html , providedCompile ) If You don't want to use the 'war' plugin, there is also an opened JIRA issue regarding 'provided' scope http://issues.gradle.org/browse/GRADLE-784 , suggested workaround is to create Your own cofiguration:
configurations {
provided
}
and set it to be used with your compilation classpath:
sourceSets {
main {
compileClasspath += configurations.provided
}
}
Check your app level gradle file. If any line looks like this:
compile dependency.gson provided dependency.javaxAnnotation
Edit it like this:
compile dependency.gson
provided dependency.javaxAnnotation
It should work.

Categories

Resources