构建工具Maven/Gradle

构建工具(本地仓库存储 与 远程仓库的全局配置)

Maven 本地存储 与 远程仓库做镜像配置(提高下载速度)
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <localRepository>D:\MyMavenLocalRepositoryDir</localRepository>
    <mirrors>
        <mirror>
            <id>aliyunMavenCentral</id>
            <name>Aliyun Maven Central</name>
            <mirrorOf>central</mirrorOf>
            <url></url>
        </mirror>
    </mirrors>
</settings>
Gradle 本地存储 与 远程仓库 全局配置

配置环境变量,指定本地缓存

GRADLE_USER_HOME = D\MyGradleLocalDir

创建init.gradle配置脚本,存放于$GRADLE_USER_HOME目录下

allproject{
     repositories {
            // 替换maven2/jcenter为阿里仓库
            all { ArtifactRepository repo ->
                        if(repo instanceof MavenArtifactRepository){
                            def url = repo.url.toString()
                            if (url.startsWith('https://repo1.maven.org/maven2')) {
                                remove repo
                            }
                            if (url.startsWith('https://jcenter.bintray.com/')) {
                                remove repo
                            }
                        }
            }
            maven { 
                url 'http://maven.aliyun.com/nexus/content/groups/public/' 
                url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'
            }
     }
}

构建工具 的生命周期

Maven 三个生命周期
默认阶段
validate 
initialize 
generate-sources 
process-sources 
generate-resources 
process-resources
compile
process-classes
generate-test-sources
process-test-sources
generate-test-resources
process-test-resources
test-compile
process-test-classes
test
pre-integrate-test
integrate-test
post-integrate-test
verify
install
deploy
clean清理阶段
pre-clean
clean
post-clean
site阶段,生成文档
pre-site
site
post-site
site-deploy
Gradle 三个构建阶段
初始化:确定工程中有哪些项目,梳理之间的关系,同时为每个项目仓库Project实例
配置:执行每个项目的build.gradle的配置预处理,解决task之间的DAG依赖关系
执行:在上面配置阶段确定好task执行顺序后,真正地执行每个项目的task任务

构建工具 插件的使用

  • 执行Maven的生命周期阶段,会将之前的阶段都执行,比如执行compile阶段会先依次执行validate/initialize阶段
  • 每执行一个阶段,绑定在该阶段的所有插件的goal将被执行
Maven插件与阶段绑定

使用help插件查看其他插件有哪些goal

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-help-plugin
Name: Apache Maven Help Plugin
Description: ...
Group Id: org.apache.maven.plugins
Artifact Id: maven-help-plugin
Version: 3.2.0
Goal Prefix: help

This plugin has 8 goals:

help:active-profiles
  Description: 当前构建激活的配置.

help:all-profiles
  Description: 当前构建所有配置.

help:describe
  Description: 打印goals列表.

help:effective-pom
  Description: 显示当前构建有效的pom,以xml显示.

help:effective-settings
  Description: ...

help:evaluate
  Description: ...

help:help
  Description: 查看具体goal有哪些参数.

help:system
  Description: 显示系统属性与环境变量列表.

For more information, run 'mvn help:describe [...] -Ddetail'

查看goal的参数信息,带上-Ddetail

mvn help:help -Ddetail=true -Dgoal=system

单独执行help插件system Goal ,打印系统环境变量

mvn help:system 

编写一个空的pom.xml,绑定help:system goal到compile阶段

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>help-plugin-bind-2-compile-phase</artifactId>
    <groupId>cn.gd.gz.jim</groupId>
    <version>1.0-SNAPSHOT</version>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-help-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>helpPluginBind</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>system</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

执行compile阶段,验证是否执行了help:system goal

mvn compile

结果验证,执行maven-compiler-plugin:3.1:compile后,执行maven-help-plugin:3.1.1:system (helpPluginBind)

[INFO] Scanning for projects...
[INFO]
[INFO] -----------< cn.gd.gz.jim:help-plugin-bind-2-compile-phase >------------
[INFO] Building help-plugin-bind-2-compile-phase 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ help-plugin-bind-2-compile-phase ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\ProgramFiles\apache-maven-3.6.0\bin\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ help-plugin-bind-2-compile-phase ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-help-plugin:3.1.1:system (helpPluginBind) @ help-plugin-bind-2-compile-phase ---
[INFO]
===============================================================================
========================= Platform Properties Details =========================
===============================================================================

===============================================================================
System Properties
===============================================================================

java.runtime.name=Java(TM) SE Runtime Environment
......

===============================================================================
Environment Variables
===============================================================================

PROCESSOR_LEVEL=6
......
Gradle插件与任务的执行
  • Gradle的插件封装了一批tasks任务,configurations配置等信息

在某目录下创建空白build.gradle,并执行下面命令,查看gradle选项参数

gradle --help
--daemon 以守护进程方式构建
-c settings.gradle文件位置
-b build.gradle文件位置
-Dk=v 系统属性
-Pk=v 项目属性
......等

执行以下命令,查看如何执行任务/查看任务详情等信息

gradle help
Starting a Gradle Daemon (subsequent builds will be faster)

> Task :help
Welcome to Gradle 5.2.1.
To run a build, run gradle <task> ...
To see a list of available tasks, run gradle tasks
To see a list of command-line options, run gradle --help
To see more detail about a task, run gradle help --task <task>
For troubleshooting, visit https://help.gradle.org

执行任务,如上面gradle help一样,执行gradle + taskName

查看当前项目下可用的任务列表

gradle tasks

显示空白build.gradle项目,Gradle自带的基本taskS

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'XXX'.
components - Displays the components produced by root project 'XXX'. [incubating]
dependencies - Displays all dependencies declared in root project 'XXX'.
dependencyInsight - Displays the insight into a specific dependency in root project 'XXX'.
dependentComponents - Displays the dependent components of components in root project 'XXX'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'XXX'. [incubating]
projects - Displays the sub-projects of root project 'XXX'.
properties - Displays the properties of root project 'XXX'.
tasks - Displays the tasks runnable from root project 'XXX'.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

通过以下命令查看init task的任务详情信息(用于初始化一个gradle项目,通过指定一些参数)

gradle help --task init
> Task :help
Detailed task information for init
Path
     :init
Type
     InitBuild (org.gradle.buildinit.tasks.InitBuild)
Options
     --dsl     Set the build script DSL to be used in generated scripts.
               Available values are:
                    groovy
                    kotlin
     --package     Set the package for source files.
     --project-name     Set the project name.
     --test-framework     Set the test framework to be used.
                          Available values are:
                               junit
                               kotlintest
                               scalatest
                               spock
                               testng
     --type     Set the type of project to generate.
                Available values are:
                     basic
                     cpp-application
                     cpp-library
                     groovy-application
                     groovy-library
                     java-application
                     java-library
                     kotlin-application
                     kotlin-library
                     pom
                     scala-library
Description
     Initializes a new Gradle build.
Group
     Build Setup

任务的执行顺序

使用java插件,往空白build.gradle添加以下内容

apply plugins: 'java'

执行如下命令,查看task

gradle tasks

多了3个组的任务:Build tasks / Documentation tasks / Verification tasks

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

查看执行 build 任务之前执行了什么任务?

gradle build -m
:compileJava SKIPPED
:processResources SKIPPED
:classes SKIPPED
:jar SKIPPED
:assemble SKIPPED
:compileTestJava SKIPPED
:processTestResources SKIPPED
:testClasses SKIPPED
:test SKIPPED
:check SKIPPED
:build SKIPPED

构建工具查看依赖树

往build.gradle添加依赖test配置,添加的内容如下(需要配置repositories,下载依赖)

repositories {
            maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
            maven{ url 'http://maven.aliyun.com/nexus/content/repositories/jcenter'}
}

dependencies{
   implementation "org.springframework.boot:spring-boot-starter-web"
   runtimeOnly "com.h2database:h2"
   testImplementation "org.springframework.boot:spring-boot-starter-test"
}

Maven/Gradle查看依赖树

mvn dependency:tree
gradle dependencies [--configuration implementation]

扩展:

1。更改任务的执行时序

2。自定义任务
方式一:build.gradle中写 
方式二:做成插件(插件开发)

3。监听Gradle构建过程

4。Gradle自定义configuration,如integrationTest(默认main/test)

5。Maven自定义插件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值