How to install spring-boot-starter-security dependency - java

I am reading a tutorial on "Building Microservices with Spring", however, I can't find "spring-boot-starter-security" dependency - I am using Spring Tool Suite 3.8.3
Any guidance on how to install this dependency will be highly appreciated.
PS: I am new to Java and SpringFramework
Maven dependency management picture

You could also right-click on the pom.xml file and go to "Spring -> Edit Starters" and select the one for spring security there. It adds that spring boot starter dependency to your pom for you.

You need to find your pom.xml, this is the file where all your dependencies are managed.
You can find a file in the Spring Tool Suite, by going to the search menu and d selecting file. You simply then need to enter "pom.xml" in the "File name patterns (seperated by comma):" field and press search.
You then can add the dependency like this in the <dependencies> </dependencies> element of the xml file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>1.4.0.M3</version>
</dependency>

Related

How to add a non-spring-boot dependency to the pom.xml in a spring-boot project

I have a Spring Boot project and I want to use there the dependency to the ModelMaker:
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.6</version>
</dependency>
If I add it, I get the error message:
Save could not be completed. Try File > Save As... if the problem persists.
Reason:
Could not write file pom.xml
details:
Could not write file: D:\...\pom.xml.
D:\...\pom.xml (The requested operation cannot be performed on a file with a user-mapped section open)
The ModelMapper does not belong to the standard Spring Boot starters, so I cannot add it to pom.xml by Spring>Edit Starters
How can I add a non-starter dependency?
Today I found the same problem.
If you are using IntelliJ, just right click in the pom.xml, then Maven and then Reimport.

how to create jar from spring boot project , this jar we want to use in another spring boot app?

We are planning to create one spring boot project , which we create as jar file , and this jar we will use as dependency for another spring boot application , point is the first jar must contains the classes we code and its related jar files , is it possible like that ?can any one help us please .
Problem is when i create the jar , only my classes which i coded are packing into jar , not the dependencies of that app
Just use spring-boot-maven-plugin or spring-boot-gradle-plugin depending on your build system. Purpose of these plugins is exactly your use case.
Spring Guides are great place to start exploring: Maven Guide, Gradle Guide.
The only caveat is that you want to exclude tomcat internal container from such shared library:
Maven:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- ... -->
</dependencies>
Gradle:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
...
}
You can do that by using "maven shade" plugin for creating a Fat jar, also called uber-jar of the parent project, which you want to use in child project. This Fat jar contains the code you have written along with all the dependencies of the project. You can check here for more details : https://maven.apache.org/plugins/maven-shade-plugin/index.html. The Fat jar created will be placed in your local repository.
In your child project, you can use parent project as a dependency by specifying following properties:
<dependency>
<groupId>parent_project_group_id</groupId>
<artifactId>parent_project_artifact_id</artifactId>
<version>parent_project_version</version>
<dependency>
Make sure that you build parent project with maven shade plugin and not with spring boot default plugin. Also, use "clean install" as goal while building the parent project.

using spring boot in a maven project

I am starting a new project with maven and spring boot and got problems already at the very beginning :-) Finally it works now, but I would like to know what I did wrong:
I simply wanted to add spring boot to my project by adding the maven dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-build</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
which can be found in Maven-Central.
This doesn't work.
Then I found out, that the correct dependency is without "-build":
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.5.9.RELEASE</version>
</dependency>
I would like to understand, where is the problem:
Do I get the info on the wrong place and maven central is wrong ?
Or why did I get problems from beginning. What is the correct aproach ?
I think one of the best approaches for creating new SpringBoot projects is to use the Spring Initilizr: https://start.spring.io/. With this tool you will get a generated .zip file which containts the basic maven/gradle folder structure and has the correct dependencies in your pom.xml or build.gradle file. During the creation of the .zip you can easily search for you required SpringBoot dependencies like Web or Security and with a tick in the checkbox will get you the right dependency.
You can also use a Plugin for Eclipse called Spring Tool Suite which enables you to create fresh SpringBoot project out of eclipse with rightclick -> New -> New Spring Starter Project. This is also available in IntelliJ or you use the dedicated IDEA STS from Eclipse https://spring.io/tools/sts/all

Project in Spring Tool Suite report "Unable to load annotation processor factory"

My project in Spring Tool Suite report Unable to "load annotation processor factory 'org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor' for project XXX", My project runs fine, but I do not want to see this error.I did a lot of searching, but I could not find any result.
Thanks a lot.
Delete the .factory file from the project root directory.
You need to add the spring boot configuration processor to your project; when using maven add this dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
You find more information in the spring boot documentation.

How to add Spring libs using Maven

I learned Spring via Spring In Action 3 few month ago. I downloaded Spring libraries from official site (list was like in SIA3(aop, asm, aspects, beans ...)), added them to my project and everything worked fine. Now I want to use Maven, but I am getting a lot of errors and sinking in searching what library to add.
I am newby, dont know all Spring dependencies(within it libs) and the question is not about my errors, but about the way to add all Spring libraries to my project via Maven. How do you usually add Spring libs using Maven?
You don't have to download the libraries themselves anymore. That is what Maven is for. (and quite some more, of course)
set up Maven properly
set up Maven in the IDE tool you have (like this)
edit the pom.xml to include what you need, adding the dependencies in the in the dependencies tag.
Maven takes care of resolving the dependencies of the specified packages. If a package depends on other packages, it will do it for you. You only have to specify the packages you directly need
For example
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
<version>2.5.6</version>
</dependency>
You can easily find the packages using Google, and searching for "maven repository "
Avoiding version clashes
Also, as Bart mentioned, the common way of having Spring in the pom.xml - as it has way too many versions, and clashes can occur - is through a common property specifying the version for all Spring components. (Based on this answer)
Specify the property in the properties tag:
<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties>
Then use it in the dependencies like this:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
Be careful to use it for ALL components to avoid version clashes. (of course, issues mught still occur, bz having different libraries reference spring too, but that is another story in its own.)
Side note
Keep in mind note that Maven projects use specific directory layout. When I first started using maven for my own projects, first I created a new blank one, and played around with it, before I began migrating my older projects to use maven. Believe me, it pays off.
Add spring artifacts to your pom.xml file. For example
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>
You can find more artifact info here
http://mvnrepository.com/
HERE you can find the dependencies as per your requirement. Just click on the dependency and inside click on the latest release, scroll down there is your code inside the <dependencies> Your required dependency and version</dependencies>.
Just copy the XML code and paste it in your pom.xml file.

Categories