maven项目与gradle项目比较

8 篇文章 0 订阅
8 篇文章 0 订阅

基于spring boot 创建maven项目

创建maven项目

  1. 打开idea,并点击Creat New Project
    在这里插入图片描述
  2. 修改Group(般是公司域名)和Artifact(项目名称),选择type为maven

在这里插入图片描述

  1. 选择develop tools 中所有包和web 中 spring web

在这里插入图片描述

创建 gradle 项目

与maven项目创建主要区别在上面步骤2,创建gradle 项目type选择 gradle即可
在这里插入图片描述

maven 与gradle 依赖管理

maven pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.herion</groupId>
    <artifactId>maven-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>maven-demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

gradle build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'com.herion'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}

Gradle在设计的时候基本沿用了Maven的这套依赖管理体系。不过它在引用依赖时还是进行了一些改进。首先引用依赖方面变得非常简洁。

Maven与Gradle依赖项scope

Maven和Gradle对依赖项的scope有所不同。在Maven世界中,一个依赖项有6种scope而gradle的scope 一直在优化调整优化。

maven scope属性

依赖范围控制哪些依赖在哪些classpath 中可用,哪些依赖包含在一个应用中。

  • compile (编译)
    compile是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的classpath中可用,同时它们也会被打包。

  • provided (已提供)
    provided 依赖只有在当JDK 或者一个容器已提供该依赖之后才使用。例如, 如果你开发了一个web 应用,你可能在编译 classpath 中需要可用的Servlet API 来编译一个servlet,但是你不会想要在打包好的WAR 中包含这个Servlet API;这个Servlet API JAR 由你的应用服务器或者servlet 容器提供。已提供范围的依赖在编译classpath (不是运行时)可用。它们不是传递性的,也不会被打包。

  • runtime (运行时)
    runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC
    驱动实现。

  • test (测试)
    test范围依赖 在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

  • system (系统)
    system范围依赖与provided类似,但是你必须显式的提供一个对于本地系统中JAR文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构建应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个systemPath元素。注意该范围是不推荐使用的(建议尽量去从公共或定制的 Maven 仓库中引用依赖)

  • import(导入)
    import仅支持在中的类型依赖项上。它表示要在指定的POM 部分中用有效的依赖关系列表替换的依赖关系。该scope类型的依赖项实际上不会参与限制依赖项的可传递性。

gradle 依赖管理 scope(gradle 6.6.1)

依赖管理介绍参考官方

  • compile (已弃用)
    编译时间依赖性。被取代implementation。

  • implementation 延伸 compile
    implementation 默认的scope。implementation的作用域会让依赖在编译和运行时均包含在内,但是不会暴露在类库使用者的编译时。举例,如果我们的类库包含了gson,那么其他人使用我们的类库时,编译时不会出现gson的依赖。

  • compileOnly
    仅编译时依赖项,在运行时不使用。编译时可见

  • compileClasspath 延伸 compile, compileOnly, implementation
    编译类路径,在编译源代码时使用。由任务使用compileJava。

  • annotationProcessor
    编译期间使用的注释处理器。

  • runtime (不建议使用)扩展 compile
    运行时依赖项。被取代runtimeOnly。

  • runtimeOnly
    仅运行时依赖项,运行时可见

  • runtimeClasspath 延伸 runtimeOnly, runtime, implementation
    运行时类路径包含实现的元素以及仅运行时元素。

  • testCompile (不建议使用)扩展 compile
    编译测试的其他依赖项。被取代testImplementation。

  • testImplementation 延伸 testCompile, implementation
    仅实现测试的依赖项。

  • testCompileOnly
    其他依赖项仅用于编译测试,在运行时不使用。

  • testCompileClasspath 延伸 testCompile, testCompileOnly, testImplementation
    测试编译类路径,在编译测试源时使用。由任务使用compileTestJava。

  • testRuntime (不建议使用)扩展 runtime, testCompile
    仅用于运行测试的其他依赖项。被取代testRuntimeOnly。

  • testRuntimeOnly 延伸 runtimeOnly
    运行时仅依赖于运行测试。

  • testRuntimeClasspath 延伸 testRuntimeOnly, testRuntime, testImplementation
    用于运行测试的运行时类路径。由任务使用test。

  • archives
    Artifacts (e.g. jars) produced by this project. Used by task uploadArchives.

  • default 延伸 runtimeClasspath
    项目依赖于此项目的默认配置。包含此项目在运行时所需的工件和依赖项

下图分别显示了main和test 的依赖项配置。您可以使用此图例解释颜色:
  • 灰色文本-已弃用配置。

  • 绿色背景-您可以声明对配置的依赖关系。

  • 蓝灰色背景-该配置供任务使用,而不供您声明依赖项。

  • 浅蓝色背景与等宽线文本— —一个任务。

    java插件- main 依赖项配置
    图一 java插件- main 依赖项配置

Java插件- test依赖项配置
图二 Java插件- test依赖项配置

Maven与Gradle 灵活性,性能,用户体验

从maven 迁移到gradle

从ant迁移到gradle

gradle 其他参考

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值