Gradle 创建 Java Library

1,029 阅读1分钟

1. 生成骨架

gradle init

# 生成的工程架构
library

# 语言 
java

# 打包 DSL
Groovy

# 测试框架
Junit4

2. 打包

./gradlew build

3. 自定义

# 自定义打包版本
version = 1.0.0

# 自定义打包的 MANIFEST
tasks.named('jar') {
    manifest {
        attributes('Implementation-Title': project.name,
                   'Implementation-Version': project.version)
    }
}

# 自定义打包出 doc 和 源码. 本身java-libary这个插件就具有打包doc和source的功能,这里只是开启
java {
    withJavadocJar()
    withSourcesJar()
}

4. 最终的build.gradle

api 和 implementation 的区别见注释

plugins {
  // Apply the java-library plugin for API and implementation separation.    
  id 'java-library'
}
// 指定版本
version = '1.0.0'
java {
  // 生成 源码    
  withSourcesJar()
  // 生成 Doc    
  withJavadocJar()
}
repositories {
  // Use JCenter for resolving dependencies.
  //    jcenter()    
  maven{ url 'https://repo.huaweicloud.com/repository/maven/'}
}
dependencies {
  // Use JUnit test framework.    
  testImplementation 'junit:junit:4.13'    
  // This dependency is exported to consumers, that is to say found on their compile classpath.    
  api 'org.apache.commons:commons-math3:3.6.1'    
  // This dependency is used internally, and not exposed to consumers on their own compile classpath.    
  implementation 'com.google.guava:guava:29.0-jre'
}

4. 参考:

docs.gradle.org/current/sam…