In Maven, make a web project depend on Java project

Make Web projects depend on Java projects

Article directory

  • Let Web projects depend on Java projects
    • 1. Concept
    • 2. Operation
    • 3. In the web project, write the test code
      • ①Supplementary creation directory
      • ②Confirm that the Web project depends on junit
      • ③ Create a test class
    • 4. Execute the Maven command
      • ①Test command
      • ②Package command
      • ③ View the list of jar packages that the current Web project depends on
      • ④ View the dependency information of the current Web project in a tree structure
    • 5. Finally: thanks

1. Concept

A clear awareness: Only Web projects have ever depended on Java projects, and Java projects have never depended on Web projects. Essentially, the Java project that the Web project depends on is actually the jar package imported in the Web project. Finally, the Java project will become a jar package and be placed in the WEB-INF/lib directory of the Web project.

The next thing we need to do is to import the jar package of the pro01-maven-java project we wrote into our current pro02-maven-web used in engineering.

2. Operation

In the pom.xml of the pro02-maven-web project, find the dependencies tag, and configure the following in the dependencies tag:

<!-- Configure dependencies on Java project pro01-maven-java -->
<!-- Specific configuration method: use coordinates to implement dependencies in the dependency tag -->
<dependency>
  <groupId>com.rainbowSea.maven</groupId>
  <artifactId>pro01-maven-java</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

3. In web engineering, write test code

Prove that the class created in the Java project can be used normally in the Web project: Calculator

①Supplementary creation directory

pro02-maven-web\src\main\java\com\rainbowSea\maven

You can use the mkdir command:

mkdir src\test\java\com\rainbowSea\maven

②Confirm that the Web project depends on junit

<!--The import is the dependency on the packaged jar in pro01-maven-java-->
<dependency>
<!--Complete dependency by specifying the coordinates of the dependent project -->
<groupId>com.rainbowSea.maven</groupId>
      <artifactId>pro01-maven-java</artifactId>
      <version>1.0-SNAPSHOT</version>
<!--The scope of the life cycle-->
<scope>compile</scope>
</dependency>

③Create a test class

Put the CalculatorTest.java test class written in the Java project into the pro02-maven-wb\src\test\java\com\rainbowSea\maven directory

package com.rainbowSea.maven;


import org.junit.Test;
import com.rainbowSea.maven.Calculator;
  
// The effect of static import is to import the static resources in the Assert class into the current class
// In this way, the static resources in the Assert class can be directly used in the current class without writing the class name
import static org.junit.Assert.*;
  
public class CalculatorTest{<!-- -->
  
  @Test
  public void testSum(){<!-- -->
    
    // 1. Create Calculator object
    Calculator calculator = new Calculator();
    
    // 2. Call the method of the Calculator object to get the actual result of the program running
    int actualResult = calculator. sum(5, 3);
    
    // 3. Declare a variable to indicate the expected result of the program running
    int expectedResult = 8;
    
    // 4. Use assertions to determine whether the actual result is consistent with the expected result
    // If consistent: the test passes without throwing an exception
    // If inconsistent: throw exception, test fails
    assertEquals(expectedResult, actualResult);

    System.out.println("test method have been called ,Hello Mavne");
    
  }
  
}

4. Execute Maven command

①Test command

mvn test # Run the test program, run the test program, the program will be automatically compiled first.

Note: The compilation operation will be automatically executed in advance during the test operation. If the test is successful, it means that the compilation is also successful.

②Package command

mvn clean package # Clear the target first, and then package to get the latest one.

Package our project into a ‘war’

By looking at the structure in the war package, we can see that the Java project that the Web project depends on will indeed become a jar package in the WEB-INF/lib directory of the Web project.

③View the list of jar packages that the current web project depends on

mvn dependency:list # Display the jar packages that the project depends on in the form of a list

[INFO] The following files have been resolved:
[INFO] junit:junit:jar:4.11:test
[INFO] org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] com.rainbowSea.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile

Description: The format of javax.servlet:javax.servlet-api:jar:3.1.0:provided displays the coordinate information of a jar package. The format is:

javax.servlet means: groupld coordinate vector

javax.servlet-api expresses: artfactld coordinate vector

jar represents the packaging method

3.1.0 represents the version version

provided indicates the scope of the dependency.

Although this format is different from the coordinate format in our XML configuration file, it is still coordinate information in essence. You need to be able to understand this format. You will see this in the logs or error messages of Maven commands in the future. Format information, it can be recognized that this is the coordinates. Then go to the Maven warehouse to find the corresponding jar package according to the coordinates, and use this method to solve the error reporting situation we encountered.

④View the dependency information of the current web project in a tree structure

mvn dependency:tree # View information about current project dependencies in a tree structure

[INFO] --- maven-dependency-plugin:2.8:tree (default-cli) @ pro02-maven-web ---
[INFO] com.rainbowSea.maven:pro02-maven-web:war:1.0-SNAPSHOT
[INFO] + - junit:junit:jar:4.11:test
[INFO] | \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] + - javax.servlet:javax.servlet-api:jar:3.1.0:provided
[INFO] \- com.rainbowSea.maven:pro01-maven-java:jar:1.0-SNAPSHOT:compile

We didn’t depend on hamcrest-core in pom.xml , but it was added to our dependency list. The reason is: junit depends on hamcrest-core, and then based on the transitivity of dependencies, hamcrest-core is passed to our project.

5. Finally: Thanks

This article refers to the following blogger’s sharing. Here again, we sincerely thank the bloggers for their enthusiastic sharing of their technology.

Thanks to the following bloggers for sharing

[1]: Weapon | Code Rework