Gradle创建Java类库


Gradle Init
我们使用 gradle init 命令来创建java类库。
在任意位置新建文件夹 “javaLib”
打开命令行工具,将目录切换到 “javaLib” 下。
执行命令:gradle init
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] 7
我们选择要创建的项目类型,我们输入7,后面就一直使用默认配置即可。
Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: groovy) [1..2]
Select test framework:
1: junit
2: testng
3: spock
Enter selection (default: junit) [1..3]
Project name (default: javaLib):
Source package (default: javaLib):
BUILD SUCCESSFUL in 39s
2 actionable tasks: 2 executed
这样一个简单的java类库就创建完毕了。
类库默认配置
java库创建完毕后,我们观察一下其默认配置情况。
settings.gradle
rootProject.name = 'javaLib'
在 settings.gradle 下配置了我们的根项目名称。
build.gradle
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// 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:27.0.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
在 build.gradle 下配置了 “java-library” 插件,还有仓库,以及项目依赖的其它库。
生成jar包
执行gradlew build
命令即可。生成的jar包在目录 javaLib\build\libs
下。
jar包名称为:javaLib.jar
为jar包指定版本号
修改 build.gradle 文件,添加一行设置 version = '0.1.0'
build.gradle
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// 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:27.0.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
gl>version = '0.1.0'
重新运行命令 gradlew build
,在目录 javaLib\build\libs
下会生成一个名为“javaLib-0.1.0.jar”的文件。
为jar包的Meta信息添加版本号
修改 build.gradle 文件,添加如下高亮代码:
build.gradle
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving your dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// 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:27.0.1-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.12'
}
version = '0.1.0'
gl>jar {
gl> manifest {
gl> attributes('Implementation-Title': project.name,
gl> 'Implementation-Version': project.version)
gl> }
gl>}
重新运行命令 gradlew build
打开javaLib-0.1.0.jar文件,javaLib-0.1.0\META-INF\MANIFEST.MF 内容如下:
Manifest-Version: 1.0
Implementation-Title: javaLib
Implementation-Version: 0.1.0
生成JavaDoc
运行命令gradlew javadoc
,在目录javaLib\build\docs\javadoc
下就会看到生成的doc文档。打开下面的 index.html 即可。
扫码分享
版权说明
作者:SQBER
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
{0}
{5}
{1}
{2}回复
{4}
*昵称:
*邮箱:
个人站点:
*想说的话: