Gradle创建Java类库

时间 2019/4/22 22:52:15 加载中...

Gradle Init

我们使用 gradle init 命令来创建java类库。

在任意位置新建文件夹 “javaLib”
打开命令行工具,将目录切换到 “javaLib” 下。

执行命令:gradle init

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

我们选择要创建的项目类型,我们输入7,后面就一直使用默认配置即可。

  1. Select build script DSL:
  2. 1: groovy
  3. 2: kotlin
  4. Enter selection (default: groovy) [1..2]
  5. Select test framework:
  6. 1: junit
  7. 2: testng
  8. 3: spock
  9. Enter selection (default: junit) [1..3]
  10. Project name (default: javaLib):
  11. Source package (default: javaLib):
  12. BUILD SUCCESSFUL in 39s
  13. 2 actionable tasks: 2 executed

这样一个简单的java类库就创建完毕了。

类库默认配置

java库创建完毕后,我们观察一下其默认配置情况。

settings.gradle

  1. rootProject.name = 'javaLib'

在 settings.gradle 下配置了我们的根项目名称。

build.gradle

  1. plugins {
  2. // Apply the java-library plugin to add support for Java Library
  3. id 'java-library'
  4. }
  5. repositories {
  6. // Use jcenter for resolving your dependencies.
  7. // You can declare any Maven/Ivy/file repository here.
  8. jcenter()
  9. }
  10. dependencies {
  11. // This dependency is exported to consumers, that is to say found on their compile classpath.
  12. api 'org.apache.commons:commons-math3:3.6.1'
  13. // This dependency is used internally, and not exposed to consumers on their own compile classpath.
  14. implementation 'com.google.guava:guava:27.0.1-jre'
  15. // Use JUnit test framework
  16. testImplementation 'junit:junit:4.12'
  17. }

在 build.gradle 下配置了 “java-library” 插件,还有仓库,以及项目依赖的其它库。

生成jar包

执行gradlew build命令即可。生成的jar包在目录 javaLib\build\libs 下。
jar包名称为:javaLib.jar

为jar包指定版本号

修改 build.gradle 文件,添加一行设置 version = '0.1.0'

build.gradle

  1. plugins {
  2. // Apply the java-library plugin to add support for Java Library
  3. id 'java-library'
  4. }
  5. repositories {
  6. // Use jcenter for resolving your dependencies.
  7. // You can declare any Maven/Ivy/file repository here.
  8. jcenter()
  9. }
  10. dependencies {
  11. // This dependency is exported to consumers, that is to say found on their compile classpath.
  12. api 'org.apache.commons:commons-math3:3.6.1'
  13. // This dependency is used internally, and not exposed to consumers on their own compile classpath.
  14. implementation 'com.google.guava:guava:27.0.1-jre'
  15. // Use JUnit test framework
  16. testImplementation 'junit:junit:4.12'
  17. }
  18. gl>version = '0.1.0'

重新运行命令 gradlew build,在目录 javaLib\build\libs 下会生成一个名为“javaLib-0.1.0.jar”的文件。

为jar包的Meta信息添加版本号

修改 build.gradle 文件,添加如下高亮代码:

build.gradle

  1. plugins {
  2. // Apply the java-library plugin to add support for Java Library
  3. id 'java-library'
  4. }
  5. repositories {
  6. // Use jcenter for resolving your dependencies.
  7. // You can declare any Maven/Ivy/file repository here.
  8. jcenter()
  9. }
  10. dependencies {
  11. // This dependency is exported to consumers, that is to say found on their compile classpath.
  12. api 'org.apache.commons:commons-math3:3.6.1'
  13. // This dependency is used internally, and not exposed to consumers on their own compile classpath.
  14. implementation 'com.google.guava:guava:27.0.1-jre'
  15. // Use JUnit test framework
  16. testImplementation 'junit:junit:4.12'
  17. }
  18. version = '0.1.0'
  19. gl>jar {
  20. gl> manifest {
  21. gl> attributes('Implementation-Title': project.name,
  22. gl> 'Implementation-Version': project.version)
  23. gl> }
  24. gl>}

重新运行命令 gradlew build
打开javaLib-0.1.0.jar文件,javaLib-0.1.0\META-INF\MANIFEST.MF 内容如下:

  1. Manifest-Version: 1.0
  2. Implementation-Title: javaLib
  3. Implementation-Version: 0.1.0

生成JavaDoc

运行命令gradlew javadoc,在目录javaLib\build\docs\javadoc下就会看到生成的doc文档。打开下面的 index.html 即可。

扫码分享
版权说明
作者:SQBER
文章来源:http://www.sqber.com/articles/gradle-building-java-libraries.html
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。