Maven使用

maven 传递依赖机制

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html

当遇到多个版本作为依赖关系时,maven 将选择哪个版本作为真正的依赖呢? Maven选择了“最近的定义”。也就是说,它使用依赖关系树中最接近您的项目的版本。您始终可以通过在项目的POM中明确声明版本来保证版本。请注意,如果两个依赖关系版本在依赖关系树中的深度相同,则第一个声明将获胜。 例如,如果将A,B和C的依存关系定义为A-> B-> C-> D 2.0和A-> E-> D 1.0,则在构建A时将使用D 1.0,因为从A的路径通过E到D的时间更短。您可以在A中向D 2.0显式添加一个依赖项,以强制使用D 2.0。

 

Dependency Scope(依赖范围)

  • compile This is the default scope, used if none is specified. Compile dependencies are available in all classpaths of a project. Furthermore, those dependencies are propagated to dependent projects.

  • provided This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.

  • runtime This scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.

  • test This scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. This scope is not transitive.(此范围不是可传递的。)

  • system This scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

  • import This scope is only supported on a dependency of type pom in the <dependencyManagement> section. It indicates the dependency to be replaced with the effective list of dependencies in the specified POM's <dependencyManagement> section. Since they are replaced, dependencies with a scope of import do not actually participate in limiting the transitivity of a dependency.

 

Maven配置默认使用的JDK版本

问题:

  • 创建maven项目的时候,jdk版本是1.7版本,而自己安装的是1.8版本,从而导致无法使用lambda等Java8新特性。
  • 每次右键项目名-maven->update project 时候,项目jdk版本就会变回1.7版本。

在maven的安装目录找到settings.xml文件,在里面添加如下代码

复制代码
<profiles>    
  <profile> <id>jdk-1.8</id> <activation> <activeByDefault>true</activeByDefault> <jdk>1.8</jdk> </activation> <properties> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion> </properties> </profile>
</profiles>
复制代码

 

posted on 2020-03-23 10:28  快鸟  阅读(251)  评论(0编辑  收藏  举报