maven/gradle 筆記

Maven篇

1.安裝與配置(windows環(huán)境)

  • 下載zip包

maven zip download.png
  • 下載后,解壓到自己的本地目錄下

解壓.png
  • 解壓文件后的文件目錄添加到系統(tǒng)環(huán)境變量path中

例如:我的目錄路徑為:C:\maven\apache-maven-3.6.0\bin,追加到path環(huán)境變量最后,記得與之前的內(nèi)容用;隔開
以上操作完成后,maven就已經(jīng)安裝完成,win+R,輸入cmd,打開控制臺,mvn -version測試安裝完成

  • 配置(配置本地倉庫路徑,配置使用國內(nèi)鏡像)

打開settings.xml(該文件位于下載的zip文件解壓后的文件conf目錄中),例如我的文件路徑為:C:\maven\apache-maven-3.6.0\conf,增加兩部分內(nèi)容,一部分為本地倉庫地址設(shè)置:
<localRepository>C:\Users\Administrator\.m2\repository</localRepository>
一部分為配置使用國內(nèi)鏡像(使用的阿里鏡像):

<mirror>
        <id>alimaven</id>
        <name>aliyun maven</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <!--mirrorOf>central</mirrorOf-->   
        <mirrorOf>*</mirrorOf>
    </mirror>

至此,maven的所有的安裝配置完成,可以在開發(fā)中使用了,還有一點(diǎn)需要注意的,eclipe中,由于maven的配置默認(rèn)使用的是用戶的默認(rèn)設(shè)置,需要在使用的時候指定一下自己具體的配置文件。


settings配置在eclipe中的設(shè)置.png

Gradle篇

1.安裝(Mac環(huán)境)
brew install gradle
gradle -v
2.創(chuàng)建項(xiàng)目
mkdir GradleExample
cd GradleExample
gradle init
cmd界面會給出需要創(chuàng)建的項(xiàng)目類型


Select type of project to generate:
  1: basic
  2: cpp-application
  3: cpp-library
  4: groovy-application
  5: groovy-library
  6: java-application
  7: java-library
  8: kotlin-application
  9: kotlin-library
  10: scala-library
Enter selection (default: basic) [1..10]

創(chuàng)建一個java程序,選擇6

Select build script DSL:
  1: groovy
  2: kotlin
Enter selection (default: groovy) [1..2]

選擇腳本語言,默認(rèn)選擇groovy

Select test framework:
  1: junit
  2: testng
  3: spock
Enter selection (default: junit) [1..3]

選擇測試框架,默認(rèn)選擇junit

Project name (default: GradleExample):

工程名,默認(rèn)是當(dāng)前目錄的目錄名

Source package (default: GradleExample):

源碼包名,默認(rèn)也是當(dāng)前目錄的目錄名
完成后,項(xiàng)目創(chuàng)建成功

BUILD SUCCESSFUL in 4m 10s
2 actionable tasks: 2 executed

項(xiàng)目目錄結(jié)構(gòu):


image.png

當(dāng)我們需要將項(xiàng)目打包成jar包發(fā)布時,需要在build.gradle編譯配置文件中,加入項(xiàng)目編譯的相關(guān)配置

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java project to get you started.
 * For more details take a look at the Java Quickstart chapter in the Gradle
 * User Manual available at https://docs.gradle.org/5.4.1/userguide/tutorial_java_projects.html
 */

plugins {
    // Apply the java plugin to add support for Java
    id 'java'

    // Apply the application plugin to add support for building an application
    id 'application'
}

repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

dependencies {
    // This dependency is found on compile classpath of this component and consumers.
    implementation 'com.google.guava:guava:27.0.1-jre'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'
}

// Define the main class for the application
mainClassName = 'GradleExample.App'

apply plugin: 'java'
 
jar {
    baseName = 'first-gradle-demo'
    version =  '0.1.0'
    manifest {
        attributes 'Main-Class': 'GradleExample.App'
    }
}

執(zhí)行命令
./gradlew build
編譯完成,目錄結(jié)構(gòu)

image.png

生成的jar包在libs目錄下
當(dāng)然,gradle還有其他很多豐富的功能,后續(xù)會整理進(jìn)來

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容