Spring Tool Suite can't import spring library classes - java

windows 8.1 64bit, jdk 1.8 64bit, spring tool suite 3.8.1 (64bit) installed--- how can i remove red lines from import library files
image link :: http://i.stack.imgur.com/EsKWh.png

You have a maven project. In such a project the libs (dependencies) are controlled mavens pom.xml
If you have not done jet, then add the needed dependency declartions to pom
Then click on your project "demo" with the right mouse button, and click on Maven/Update Project...
A typical minimal pom.xml needed an simple spring-boot-web project looks like this (you can find a lot of other boot poms at https://spring.io/guides/):
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.your.group</groupId>
<artifactId>your-project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Related

GitLab .gitlab-ci.yml -> run mvn -Pnative spring-boot:build-image to build SpringBoot native image

Small question regarding GitLap and SpringBoot 3 native images with GraalVM please.
I am new to GitLab, got a fresh GitLab instance.
I do not think the GitLab instance got GraalVM installed.
With that said, here is the pom of my project:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/>
</parent>
<groupId>com.question</groupId>
<artifactId>spring</artifactId>
<version>1.1</version>
<name>spring</name>
<description>Spring Boot</description>
<properties>
<java.version>17</java.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I am very sure if I run mvn -Pnative spring-boot:build-image on my local machine, I get a docker image of the SpringBoot app with native binary. I can run the native binary fine.
I would like to just create the same, using GitLab.
What did I try:
So far, I am trying to follow tutorials on how to build SpringBoot apps with GitLab. However, it is dealing with regular SpringBoot non-native, non GraalVM apps, and it requires a Dockerfile. There is no Dockerfile in my project, since I am generating the Docker image directly using mvn -Pnative spring-boot:build-image
May I ask what is the command to add to .gitlab-ci.yml in order to build a native SpringBoot app please?
Thank you

Generated Spring pom.xml causes error "Error while fetching http://maven.apache.org/POM/4.0.0"

(I am aware that there are similar questions - but having tried the solutions to them I am still stuck)
I am trying to use the Demo project from Spring and cannot get the generated pom.xml to work under IntelliJ, I tried the solution suggested by IntelliJ to resolve URI is not registered - but this simply causes the titular error.
At first I believed this to be a network error, but upon loading the webpage in a browser it 404s. This leads me to believe it is an issue with my project setup in some way.
I imported the project by opening the pom.xml in IntelliJ and then clicking 'Open as Project'.
My pom.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>prs.ttg.spring</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>Test project for Spring Boot</description>
<properties>
<java.version>15</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
None of the dependencies get loaded and I cannot for the life of me find a working solution to this.
I appreciate help, and understand if this is just a stupid question - I'm a stupid person
Make sure you have Maven bundled plugin enabled in Preferences | Plugins. If you are behind proxy - set up proxy settings in IDE: https://www.jetbrains.com/help/idea/settings-http-proxy.html
If you have Maven plugin enabled open project via File | New | Project from Existing Sources and select pom.xml file to import project from.

Spring boot properties vs maven pom packaging tag

I have been facing some problems with spring boot and maven.
It seems that the <packaging>pom</packaging> tag, when added to pom.xml, somehow makes spring completely unaware of the parent-level applications.properties configuration file. When running, spring still will show the banner and info-level logging regardless of the properties stated. Why would that be the case? Is there a way to add those properties to my parent such that all modules would operate under given configuration? Would this become an anti-pattern?
Main class:
package com.example.app; //could also be inside the app-client module
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties:
spring.main.banner-mode=off
logging.level.root=info
(parent) pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>app-client</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.</groupId>
<artifactId>app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>app</name>
<description>Demo app</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Totally agreeing with the comment of Andy on your question I like to point out, that the use of the packaging type pom is appropriate if you want to denote an artifact as some kind of module container are base project where you collect dependency information for your (sub-)modules.
However the #SpringBootApplication in your case shows us that you want to use your artifact for running business code. That on the other hand leads to packaging types like jar or war. Switch it to one of them and everything should run fine.
For more information please consult also the Maven reference for packaging.

How to inherit dependencies from other libraries in maven?

I created a custom my-commons library with maven. The commons pom contains eg the following dependency:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/>
</parent>
<groupId>de.mydomain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<!-- workaround for http://jira.codehaus.org/browse/MASSEMBLY-603-->
<maven.build.timestamp>${maven.build.timestamp}</maven.build.timestamp>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
I installed the custom library into my local repository.
In a different project, I reuse the library. It resolves correctly, so it exists in the repo:
<project ...>
<groupId>de.mydomain</groupId>
<artifactId>my-core</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>my.domain</groupId>
<artifactId>my-commons</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
Now in this project I'd like to use a class org.springframework.web.bind.annotation.ResponseBody.
Problem: the class is not in classpath. So maven complains [ERROR] cannot find symbol.
But why?? I'd expect the dependency being inherited!
Sidenote: I'm using Intellij IDEA if that matters. The same problem applies to all "inherited" libraries. The spring-web lib is just an example.
Please check if .m2 folder contains the jar you want to use or not. You can use mvn install command in commons project to generate the jar. Also you need to add commons project in your project pom that you are running.
You need to create the artifact for your commons project, through its pom.xml.
Like in my below example i created a separate project where i am using the AWS and some other dependency and then final jar, which i am using in other project :-
My AWS-client pom.xml looks like below :-
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>aws-clients</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<aws.sdk.version>1.11.104</aws.sdk.version>
</properties>
<dependencies>
<!--AWS dependency-->
<!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-dynamodb -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-dynamodb</artifactId>
<version>${aws.sdk.version}</version>
</dependency>
<dependency>
<groupId>com.mercadopago</groupId>
<artifactId>sdk</artifactId>
<version>0.3.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>mercadopago</id>
<url>https://github.com/mercadopago/sdk-java/raw/master/releases</url>
</repository>
</repositories>
</project>
Also, run mvn clean install on the commons pom, in your case.
And then in all other project where i need to include the aws-client , i use the artifact-id of above project, like below :-
<dependency>
<groupId>com.kaamkaj</groupId>
<artifactId>aws-clients</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
Let me know if you have any questions:
Found it: removing the <maven.build.timestamp>... <property> solved the problem. I don't know why it prevented the inheritance to work properly, especially as I didn't get any errors.

spring boot : The import org.springframework.jdbc.core.JdbcTemplate cannot be resolved

I am having a hard time with one of my test programs which I am trying to write using springboot. When I try to import the JdbcTemplate class in the DAO layer I get an error : The import org.springframework.jdbc.core.JdbcTemplate cannot be resolved
I am not sure what I am missing, I have checked the dependency and they look fine to me. Below is my pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vittles</groupId>
<artifactId>FoodFood</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>FoodFood</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Attached is the error which I get in my java file
Also attach is the list of jar imports .
Can some one please let me know what I am doing wrong.
delete the folder ‘spring-jdbc’ from the maven repository:
%your_path%.m2\repository\org\springframework\spring-jdbc
open Eclipse -> right click on your project -> Maven -> Update project...
P.S. pom.xml should contain the jdbc dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Looks your IDE is not updating the dependencies, try run as maven project - install.
I suggest you to use springboot, also.
I checked with your code but its fine, it have JdbcTemplate library class, seems like a simple error. try to update your project "right click project->maven->update project" or clean maven.

Categories