Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The concept and classification of Maven Scope

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the concept and classification of Maven Scope. The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn the concept and classification of Maven Scope.

Scope

Scope defines the usage phase of the class package in the project. The project phase includes: compile, run, test, and release.

Classification description

Compile: the default scope is compile, which represents the current dependency on participating in the compile, test, and run phases of the project, and is a strong dependency. When packing, it will be in the bag.

Test: this dependency is only involved in testing-related content, including the compilation and execution of test cases, such as qualitative Junit.

Runtime: dependencies participate only in usage during the run cycle. Generally speaking, this kind of class library is a class library with separate interface and implementation, such as JDBC class library, which only depends on the relevant interface when compiling, and needs specific mysql, oracle and other data drivers when it is running. The drivers of this class are all runtime class libraries.

Provided: this dependency does not need to be typed in during the packaging process. This dependency is provided by the running environment, such as tomcat or basic class libraries, etc. In fact, this dependency can participate in compilation, testing, and running cycles, which is the same as compile. The difference is that the exclude operation is performed in the packaging phase.

System: use the same as provided, except that the dependency is not extracted from the maven repository, but from the local file system, which extracts the dependency with reference to the attributes of systemPath.

Import: this is a post-maven2.0.9 attribute. Import can only be used in dependencyManagement to solve the problem of maven single inheritance. Import dependencies do not actually participate in restricting the transitivity of dependencies.

For example, runtime

The first thing to know is that the jar package dependencies introduced in Maven are compiled * .class files that can be used directly! Then, for example, the jdbc link for java of runtime type, the official connection kit has been provided. When we call it, we must load the driver through reflection to link the database.

Mysql mysql-connector-java 5.1.26 runtime

Demo that relies on JDBC is as follows:

Package com.sowhat.Demo01;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;// provides the method public class JDBCUtils {private static Connection conn=null; static {try {Class.forName ("com.mysql.jdbc.Driver"); String url= "jdbc:mysql://localhost:3306/goods?characterEncoding=utf-8" to get the connection String username= "root"; String password= "123456"; conn=DriverManager.getConnection (url,username,password);} catch (Exception e) {/ / TODO Auto-generated catch block throw new RuntimeException (e+ "Database connection failed");}} / / get the database connection object public static Connection getConnection () {return conn } / / method of shutting down the database.} systemPath:

When maven relies on local jar packages instead of jar packages in repository, sytemPath indicates the local jar package path, for example:

Type in org.hamcrest hamcrest-core 1.5 system ${basedir} / WebContent/WEB-INF/lib/hamcrest-core-1.3.jardependency

When introducing a dependency, type must be specified because the minimum information set used to match the dependency reference and the dependencyManagement part is actually {groupId,artifactId,type,classifier}. In many cases, these dependencies will refer to jar dependencies without classifier. This allows us to set the identity to {groupId,artifactId} because the default value for type is jar and the default classifier is null.

The values of type generally include jar, war, pom, etc., and declare the types of dependencies introduced

Classifier in dependency

Classifier is probably the most overlooked Maven feature, but it's really important and we need it to help plan the coordinates. Imagine a situation where there is a jar project, let's say dog-cli-1.0.jar, and the user can draw a puppy on the command line by running it. Now the user's request is that you provide a zip package that contains not only the runnable jar, but also the source code and documentation, in other words, a more formal distribution. What should the file name look like? Dog-cli-1.0.zip? It is not clear enough that just from the extension it is difficult to tell what is the default generated artifact from Maven and what is the additional configuration generation distribution. It would be best if it could be dog-cli-1.0-dist.zip. The dist here is classifier, and the default Maven generates only one component, which we call the main component, so when we want Maven to generate other subsidiary components, we can use classifier. Common classifier also includes dog-cli-1.0-sources.jar for source package, dog-cli-1.0-javadoc.jar for JavaDoc package, and so on.

Classifier represents the jar used for different environments or jdk under the same version. If this element is configured, the element name will be added to the end to find the corresponding jar, for example:

Jdk7jdk8 thank you for your reading, the above is the content of "the concept and classification of Maven Scope". After the study of this article, I believe you have a deeper understanding of the concept and classification of Maven Scope, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report