how to develop gradle application using eclipse [duplicate] - java

I have Gradle build like this
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'test.build'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.session:spring-session-core'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
and here my application
package test.build.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
When I build the project, I see there is an error like in this picture:
What is the issue? Is there any error in my Gradle build file?

Looks like you have a missing dependency. Try adding the following dependency:
implementation 'org.springframework.boot:spring-boot-starter'

Related

null pointer exception at service in spring boot using #Autowired [duplicate]

This question already has answers here:
SpringApplication.run main method
(3 answers)
Closed 1 year ago.
I have a service that has a function that prints a string :
#Service
public class Aservice {
public void write(String test) {
System.out.println(test);
}
}
I'm just trying to call this function in the main function, but it gives me a null pointer exception for the service, what am i doing wrong ?
#SpringBootApplication
public class TestApplication {
#Autowired
private Aservice aservice;
public static void main(String[] args) {
TestApplication test = new TestApplication();
test.start();
}
public void start() {
aservice.write("ddd");
}
}
here's the error:
Exception in thread "main" java.lang.NullPointerException
at com.example.test.TestApplication.start(TestApplication.java:19)
at com.example.test.TestApplication.main(TestApplication.java:15)
and here's my build.gradle:
plugins {
id 'org.springframework.boot' version '2.5.0'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
The correct way to run a Spring Boot application is to use the SpringApplication run method:
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
This will bootstrap the Spring framework, including component scanning, dependency injection, etc.

i have problem for setting java environment in windows

I have Gradle build like this
plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group = 'test.build'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'org.springframework.session:spring-session-core'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.oracle.database.jdbc:ojdbc8'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
and here my application
package test.build.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
When I build the project, I see there is an error like in this picture:
What is the issue? Is there any error in my Gradle build file?
Looks like you have a missing dependency. Try adding the following dependency:
implementation 'org.springframework.boot:spring-boot-starter'

Microservice- error in bringing up config server org.springframework.boot.context.properties.bind.BindResult

I'm getting below error when trying to bring up config server. Looks incorrect jar version
any suggestions please
Error
Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.cloud.config.server.composite.CompositeEnvironmentBeanFactoryPostProcessor.bindProperties(CompositeEnvironmentBeanFactoryPostProcessor.java:81)
The following method did not exist:
'java.lang.Object org.springframework.boot.context.properties.bind.BindResult.orElseCreate(java.lang.Class)'
The method's class, org.springframework.boot.context.properties.bind.BindResult, is available from the following locations:
*jar:file:/C:/Users/GOPU/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.4.0-M1/503562062a9baee246bb7311526d08d16ae05758/spring-boot-2.4.0-M1.jar!/org/springframework/boot/context/properties/bind/BindResult.class*
The class hierarchy was loaded from the following locations:
org.springframework.boot.context.properties.bind.BindResult: file:/C:/Users/GOPU/.gradle/caches/modules-2/files-2.1/org.springframework.boot/spring-boot/2.4.0-M1/503562062a9baee246bb7311526d08d16ae05758/spring-boot-2.4.0-M1.jar
Action:
Correct the classpath of your application so that it contains a single, compatible version of org.springframework.boot.context.properties.bind.BindResult
Build.gradle
plugins {
id 'org.springframework.boot' version '2.4.0-M1'
id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java'
}
group = 'com.cloud'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '13'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.cloud:spring-cloud-config-server'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
implementation 'org.springframework.boot:spring-boot-starter'
}
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Hoxton.RELEASE'
}
}
test {
useJUnitPlatform()
}
Mainfile
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;
#EnableConfigServer
#EnableDiscoveryClient
#SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

Intellij IDEA not generate spring-configuration-metadata-json

I wait that IntelliJ IDEA generates spring-configuration-metadata-json for me.
But it doesn't happen. I read this and this articles and this SO answer and I create a simple spring-boot application:
Gradle file:
plugins {
id 'org.springframework.boot' version '2.1.4.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
}
Property:
#Component
#ConfigurationProperties(value = "test")
public class MyProperties {
private String name;
private int age;
//getters setters
I try to build, rebuild, compile... But IDEA not create this metadata for me. What do I do wrong?

Dagger not generating Component classes

I have tried a great deal debugging this issue but unable to find the cause. Dagger simply doesn't create the DaggerComponent classes. I've checked SO for duplicates but none of the solutions provided worked.
project's build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'me.tatarka:gradle-retrolambda:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
jcenter {
url "http://jcenter.bintray.com"
}
mavenCentral()
maven {
url "https://oss.sonatype.org/content/repositories/snapshots"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app's build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'me.tatarka.retrolambda'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
defaultConfig {
applicationId "com.hr.crux"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
retrolambda {
jvmArgs '-noverify'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.1.1'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
provided 'javax.annotation:jsr250-api:1.0'
apt 'com.google.dagger:dagger-compiler:2.2'
compile 'com.google.dagger:dagger:2.2'
provided 'javax.annotation:jsr250-api:1.0'
testCompile 'junit:junit:4.12'
}
HttpModule.java
#Module
public class HttpModule {
#Provides
#Singleton
Retrofit getRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://maps.googleapis.com/maps/api/place/")
.build();
return retrofit;
}
}
HttpComponent.java
#Singleton
#Component(modules = {HttpModule.class})
public interface HttpComponent {
void inject(MainActivity activity);
}
Application.java
public class Application extends android.app.Application {
private static Application application;
private HttpComponent appComponent;
#Override
public void onCreate() {
super.onCreate();
application = this;
appComponent = //Cannot find DaggerHttpComponent
}
public static Application getInstance() {
return application;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Inject
Retrofit retrofit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Dagger is failing to generate the component class in my Application class. I've tried clean building, I've tried invalidating cache but nothing works.
You can start a fresh project by yourself instead of following the tutorial project. If you do so, here is the solution.
These two lines are responsible to generate the compile-time framework in Dagger 2.
compile 'com.google.dagger:dagger:2.14.1'//generates framework in compile time
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' //generates framework in compile time based on the annotations you provided.
Full setup Dagger
//dagger 2
compile 'com.google.dagger:dagger:2.14.1'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'
//to enable DaggerActivity, DaggerBroadcastReceiver, DaggerFragment etc classes
compile 'com.google.dagger:dagger-android:2.14.1'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.14.1'
//support libraries with dagger 2
compile 'com.google.dagger:dagger-android-support:2.14.1'
Note: You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Default Settings>search"Annotation processor"
As Other threads' Answers does NOT work for me:
I've answered here
Pref -> Editor -> File Types -> Ignore Files And Folders -> Remove "Dagger*.java;"

Categories