MediumSpring Framework part4. By Maven Build Tool… | by Spring Techie | Medium
Sitemap
Spring Techie

Spring Techie

A Java Full Stack Devloper

Spring Framework part4

By Maven Build Tool…

5 min readOct 22, 2023

--

Till now we have created spring projects in a simple way, this may not be recommended way.

Because , For every project we need lot of dependencies, and this is done by downloading jar’s and setting up in the classpath. Manually downloading and managing them is headache process.

So to get rid of using the Zandu Balm , a new Build tool is developed by Apache called Maven.

Maven makes the day-to-day work of Java developers easier and helps with the building and running of any Java-based project.

Instead of adding all the jar files directly in classpath, maven helps to manage all the dependencies in the file called pom.xml. Here we will add the required dependencies and then we will update the project. Then all jar files are loaded to Maven Dependencies folder. By Adding these Dependencies ..

 <dependencies>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>6.0.12</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.12</version>
</dependency>
</dependencies>

In Maven we have a more than 250+ project structures.

1.A webapp Structure
2.quick start structure

Maven LifeCycle: It has totally 8 phases , each will run sequentially.

  1. Validate: This step validates if the project structure is correct. For example — It checks if all the dependencies have been downloaded and are available in the local repository.
  2. Compile: It compiles the source code, converts the .java files to .class, and stores the classes in the target/classes folder.
  3. Test: It runs unit tests for the project.
  4. Package: This step packages the compiled code in a distributable format like JAR or WAR.
  5. Integration test: It runs the integration tests for the project.
  6. Verify: This step runs checks to verify that the project is valid and meets the quality standards.
  7. Install: This step installs the packaged code to the local Maven repository.
  8. Deploy: It copies the packaged code to the remote repository for sharing it with other developers.

Maven Commands:

  • mvn clean: Cleans the project and removes all files generated by the previous build.
  • mvn compile: Compiles source code of the project.
  • mvn test-compile: Compiles the test source code.
  • mvn test: Runs tests for the project.
  • mvn package: Creates JAR or WAR file for the project to convert it into a distributable format.
  • mvn install: Deploys the packaged JAR/ WAR file to the local repository.
  • mvn site: generate the project documentation.
  • mvn validate: validate the project’s POM and configuration.
  • mvn idea:idea: generate project files for IntelliJ IDEA or Eclipse.
  • mvn release:perform: Performs a release build.
  • mvn deploy: Copies the packaged JAR/ WAR file to the remote repository after compiling, running tests and building the project.
  • mvn archetype:generate: This command is used to generate a new project from an archetype, which is a template for a project. This command is typically used to create new projects based on a specific pattern or structure.
  • mvn dependency:tree: This command is used to display the dependencies of the project in a tree format. This command is typically used to understand the dependencies of the project and troubleshoot any issues.

Let’s create a project using maven

Get Spring Techie’s stories in your inbox

Join Medium for free to get updates from this writer.

step1: click File → new →Select Maven project → click next.

Then type in the filter section “org.apache.maven” their you can see may project styles. Now select →maven.archetype.quickstart-1.1 → cilck next.

Press enter or click to view image in full size
Press enter or click to view image in full size

In the above picture you have provide GroupId, ArtifactId(name of the project), packagename → then click next.

Then you will see…..

project

Step2: Adding the required jar files.

You will get all the dependencies from central Maven repository ,(https://mvnrepository.com/). Search for required depndencies, then copy the most recently used dependency.

Add your dependency in the <dependencies> tag </dependencies>

Then Update the Project By right click on project → click on Maven →click on update Project.

Press enter or click to view image in full size

Now check the forceUpdate checkbox and click on Ok.

Press enter or click to view image in full size

Now you will see all the jar files in maven Dependencies folder(if you dont get then refresh project by rightclick → look for refresh).

Step3: Create class Student.java Score.java and Test.java under the src/main/java.

Student.java

( Remember here we are doing autowiring on field.)

package com.AutowireBy_annotation.onFiled;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class Student
{
String name;
@Autowired
private Scores score;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Scores getScore() {
return score;
}

@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}

}

Score.java

package com.AutowireBy_annotation.onFiled;

public class Scores
{
private float m1;

private float m2;

public float getM1() {
return m1;
}
public void setM1(float m1) {
this.m1 = m1;
}
public float getM2() {
return m2;
}
public void setM2(float m2) {
this.m2 = m2;
}
@Override
public String toString() {
return "Scores [m1=" + m1 + ", m2=" + m2 + "]";
}

}

Create config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >

<context:annotation-config></context:annotation-config>
<bean name="score" class="com.AutowireBy_annotation.onFiled.Scores" >

<property name="m1" value="100"></property>
<property name="m2" value="120"></property>
</bean>



<bean name="stu" class="com.AutowireBy_annotation.onFiled.Student" >
<property name="name" value="Raja"></property>


</bean>
</beans>

Test.java

package com.AutowireBy_annotation.onFiled;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;



public class Test {

public static void main(String[] args)
{

ApplicationContext context=new ClassPathXmlApplicationContext("config.xml");
Student s = (Student) context.getBean("stu");
System.out.println(s.getName());
System.out.println(s.getScore());
}

}

Now Run Test class .

OUTPUT

=============================================================== ===============================================================

__________________________Thanks for Reading______________________

--

--