java项目构建工具maven和gradle

最后发布时间:2023-11-11 23:45:00 浏览量:

https://stackoverflow.com/questions/23796404/could-not-find-method-compile-for-arguments-gradle
787

Note that the compile, runtime, testCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10 (Aug 27, 2018), and were finally removed in Gradle 7.0 (Apr 9, 2021).

maven 国内源

全局配置

conf/settings.xml

<mirrors>
<!-- 阿里云的镜像 -->
	<mirror>
		<id>alimaven</id>
		<name>aliyun maven</name>
		<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
		<mirrorOf>central</mirrorOf>
	</mirror>
</mirrors>

gradle配置国内源

全局配置

找到Gradle用户主目录(可以通过环境变量GRADLE_USER_HOME配置),默认为~/.gradle,新建init.gradle文件,填入如下内容:

allprojects{
    repositories {
        def ALIYUN_REPOSITORY = 'https://maven.aliyun.com/repository/public/'
        def ALIYUN_JCENTER= 'https://maven.aliyun.com/repository/jcenter/'
        def ALIYUN_GOOGLE = 'https://maven.aliyun.com/repository/google/'
        def ALIYUN_GRADLE_PLUGIN = 'https://maven.aliyun.com/repository/gradle-plugin/'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER."
                    remove repo
                }
                if (url.startsWith('https://dl.google.com/dl/android/maven2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GOOGLE."
                    remove repo
                }
                if (url.startsWith('https://plugins.gradle.org/m2/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_GRADLE_PLUGIN."
                    remove repo
                }
            }
        }
        maven { url ALIYUN_REPOSITORY }
        maven { url ALIYUN_JCENTER }
        maven { url ALIYUN_GOOGLE }
        maven { url ALIYUN_GRADLE_PLUGIN}
    }
}

文件中配置


buildscript {
    
    repositories {
       // google()
       // jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
    dependencies {
        //classpath 'com.android.tools.build:gradle:3.2.0'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
      //  google()
      //  jcenter()
        maven { url 'https://plugins.gradle.org/m2/' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/google' }
        maven { url 'https://maven.aliyun.com/nexus/content/groups/public' }
        maven { url 'https://maven.aliyun.com/nexus/content/repositories/jcenter'}
    }
}

gradle 的更新

Note that the compile, runtime, testCompile, and testRuntime configurations introduced by the Java plugin have been deprecated since Gradle 4.10 (Aug 27, 2018), and were finally removed in Gradle 7.0 (Apr 9, 2021).

The aforementioned configurations should be replaced by implementation, runtimeOnly, testImplementation, and testRuntimeOnly, respectively.

gradle中使用Groovy

dependencies {
    implementation 'org.codehaus.groovy:groovy-all:3.0.5'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

gradle 是一套支持 Groovy 语言进行配置的打包编译系统,上面这段代码经编译之后,会变成调用 dependencies (Closure closure) 方法。

def fun1(Closure closure){
    closure()
}

//{print("aaa")}()

fun1{
    print("aaa")
}

@BaseScript(MyDSL)
import groovy.transform.BaseScript

// Something to deal with people
class Person {
    String name
    Closure method
    String toString() { "$name" }
    Person(String name, Closure cl) {
        this.name = name
        this.method = cl
        this.method.delegate = this
    }
    def greet(String greeting) {
        println "$greeting $name"
    }
}

//  and our base DSL class

abstract class MyDSL extends Script {
    def methodMissing(String name, args) {
        return new Person(name, args[0])
    }

    def person(Person p) {
        p.method(p)
    }
}


person tim {
    greet 'Hello'
}

https://stackoverflow.com/questions/37670201/why-can-quotes-be-left-out-in-names-of-gradle-tasks

groovy语法

//闭包是一个对象跟Java中的基础类型int,string,boolean一样
def aClosure = {println 'Hello Closure!!'}
def methodWithClosureParam(Closure closure){
    closure()
}
methodWithClosureParam aClosure
methodWithClosureParam(aClosure)

methodWithClosureParam{
    println 'This is another closure!!'
}

//函数的最后一个参数是闭包
def methodOneParam(Closure closure)
{
    closure()
}

def methodEndWithClosureParam(def x, Closure closure)
{
    println x
    closure()
}
methodOneParam{
    println 'One parameter' //结果:One parameter
}
methodEndWithClosureParam('Hello',{
    println 'Two parameters' //结果: Hello,Two parameters
})
methodEndWithClosureParam ('Hello')  {
    println 'Two parameters' //结果: Hello,Two parameters
}

//调用闭包
def calc = {1+2}
println calc() //结果:3
println calc.call() //结果:3
//如果没有明确使用->来定义参数列表,那么这个闭包就定义了一个隐式的参数,这个隐式参数名叫it。
def greeting = { println "Hello ${it}"}
greeting('man')  //结果:Hello man
//def greeting = {it-> println "Hello ${it}"}
//greeting('man') //结果:Hello man

def varParamsClosure = {String... args ->
    args.join(' ')
}
println varParamsClosure('Hello', 'world') //结果: Hello world

https://blog.csdn.net/u014162857/article/details/54708831

groovy MOP

给一个String类扩展一个静态方法sayHello

String.metaClass.'static'.sayHello = {
    println("hello world")
}
String.'sayHello'()
String.sayHello()

给一个String类扩展构造方法,传入一个Integer然后返回实例

String.metaClass.constructor = { Integer p ->
    new String("hello world" + p)
}

println new String(1)

实例属性/方法注入

String.metaClass.myName = "XiaoMing"

println "xx".myName

String.metaClass.sayHello = {
    println('hello world')
}

"xx".sayHello()

####<<扩展方法
当你只想对对象没有的方法进行扩展,如果对象存在某个方法那么抛出错误,可以用如下方法.如果对象父级metaclass别存在此方法那么不覆盖。

class Person {
    def test1() {
        System.out.println("invoke test1")
        return "test1"
    }
}


Person.metaClass.test1 <<{
    println("say hello")
}

那么buildscript中的repositories和allprojects的repositories的作用和区别是什么呢?
https://docs.gradle.org/current/userguide/java_plugin.html
https://docs.gradle.org/7.3/userguide/application_plugin.html
https://juejin.cn/post/6864184595244548104