Gradleビルドツールを使用した新しいオープンソースJavaプロジェクトの作成
🔔 この記事はもともと私のサイトに掲載されました.MihaiBojin.com . 🔔
When building a house, you should start with a strong foundation. Similarly, creating a new open-source project requires a certain level of organization!
大まかには、まともなプロジェクトを作るベースミニマムがあります.
Version Control System
Most projects today use git
hosted on GitHub. Other options exist, but they are beyond the scope of this article.
To create a new repository, take the following actions:
LICENSE
リポジトリ内のファイルBasic repository information
At the very least, provide essential information, in a README
, that helps your users understand what the project is about, how it can help them if they can use it, and how they can contribute to it.
LICENSE
file informs potential users about the conditions under which they can use your project. This article doesn't aim to inform about all differences between various types of licenses, but you can read more about it here .最後に
CONTRIBUTING
ガイドは、潜在的な開発者がPRSを開いて、あなたのプロジェクトで協力するのを助けます.Build tool
And now, let's get to the 'meaty' part.
Since I'm writing a Java project, this is very opinionated. For the first iteration このプロジェクトのBazel しかし、私はゼロから始めているので、私はいくつかの楽しさと使用を持っていると思ったGradle .インストール開始SDKman! Gradle :
sdk install gradle
.以前使ったことがないので、読書を始めましたgetting started guide . もう一つの役に立つページはhow to configure java projects .
私の使用IntelliJ IDEとして、Kotlin DSLを使ってGradleをサポートしています.
次に、複数のサブモジュールからなるJavaライブラリのサウンドベースを設定してみましょう.
Root directory configuration I've set up this project as a multi-project build .
私は、Aをつくることから始めました
settings.gradle.kts
ファイル名.pluginManagement/repositories
セクションでは、すべてのプロジェクトで使用するアーティファクトリポジトリの集合を表します.rootProject.name = "props"
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
Define a global version for all dependencies
I created a build.gradle.kts
file and defined a group
and version
.
group = "sh.props"
version = project.version
The version is implicitly loaded from the project and can be specified in a gradle.properties
file:
version=0.2.0-SNAPSHOT
or by passing it via the command-line, e.g., gradle -Pversion=0.3.0 ...
Create a subproject
IntelliJ can easily create subprojects:
- right-click the project and select New Module
- or press Cmd-N
- select Gradle Java module
- fill in the details and you're good to go!
Alternatively, create a new directory and inside of it:
- create a
build.gradle.kts
file - create a Maven-like directory structure:
src/main/java
,src/main/resources
,src/test/java
,src/test/resources
Include this subproject in the settings.gradle.kts
file:
include("java-props-core")
Set a java toolchain version
Edit the subproject's build.gradle.kts
file and define the following block to set the Java version to 11.
plugins {
`java-library`
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(JavaVersion.VERSION_11.toString()))
}
}
// additionally also allow incremental builds, speeding up build speed
tasks.compileJava {
options.isIncremental = true
}
Generate source JARs and Javadocs
Since props
is a library, I wanted to generate source and documentation (Javadoc) JARs.
Luckily, this is super easy to do in Gradle. Again, edit the build.gradle.kts
file:
java {
withJavadocJar()
withSourcesJar()
}
tasks.create<Zip>("docZip") {
archiveFileName.set("doc.zip")
from("doc")
}
Running tests
I chose the JUnit 5 (Jupiter) framework, for which we need to set up a few things:
First, define a new task that will run the tests when called
tasks.test {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
// limit heap usage to 1G (can we tweaked later)
// https://docs.gradle.org/7.2/userguide/java_testing.html#sec:test_execution
maxHeapSize = "1G"
}
As well as the necessary dependencies:
dependencies {
testImplementation(libs.junit.jupiter.api)
testRuntimeOnly(libs.junit.jupiter.engine)
}
The libs.junit.jupiter.api
notation represents type-safe project dependencies in Gradle. These are defined in the root project settings; see below.
Define a version catalog
Version catalogs are an incubating feature and must be explicitly enabled.
Edit the settings.gradle.kts
file and add the following section:
// enable the incubating feature
enableFeaturePreview("VERSION_CATALOGS")
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
// define the version once
version("junit", "5.7.2")
// then create aliases to each component, referencing the version above
alias("junit-jupiter-api").to("org.junit.jupiter", "junit-jupiter-api").versionRef("junit")
alias("junit-jupiter-engine").to("org.junit.jupiter", "junit-jupiter-engine").versionRef("junit")
}
}
}
Once the above is done, any tests deployed in **/src/test/java
can be run by executing gradle test
.
Code formatting
As a codebase grows, it's essential for the code to 'look' the same, as it helps give developers a consistent experience.
The chosen format (style) is less important than doing this from early on. After all, you can always change the style and reformat the codebase in one go later on!
This is, in my opinion, best done by a tool that indiscriminately auto-formats your code (as you write it)!
Gradle has a great plugin for this: spotless .プラグインのバージョンを定義
settings.gradle.kts
ファイルpluginManagement {
plugins {
id("com.diffplug.spotless").version("5.14.3")
}
}
次の設定を追加しますbuild.gradle.kts
ファイルspotless {
// generic formatting for miscellaneous files
format("misc") {
target("*.gradle", "*.md", ".gitignore")
trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}
// chose the Google java formatter, version 1.9
java {
googleJavaFormat("1.9").aosp()
// and apply a license header
licenseHeaderFile(rootProject.file("props.license.kt"))
}
}
コメントで見ることができるように、他の拡張モジュールと同様にJavaファイルをフォーマットします.Javaでは、google java formatter また、すべてのファイルにライセンスヘッダを適用します.The
props.license.kt
のコピーを含むLICENSE
ファイルをいくつかの微調整:/* ... */
) $YEAR
)/*
MIT License
Copyright (c) $YEAR Mihai Bojin
Permission is hereby granted...
*/
フォーマットのチェックgradle spotlessJavaCheck
またはgradle spotlessJavaApply
.Coding standard
Checking that a project's coding standard meets minimal quality criteria results in better code!
For this I use checkstyle
, which is also very easy to set up with Gradle:
Add the following to settings.gradle.kts :
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
// make a global version available
version("checkstyle", "9.0")
}
}
}
Then edit the subproject's build.gradle.kts
file:
plugins {
checkstyle
}
checkstyle {
// will use the version declared in the catalog
toolVersion = libs.versions.checkstyle.get()
}
Run the check with gradle checkstyleMain
.
Smarter static code analysis Finally, the last component I'll be talking about in this article is
errorprone
, a library built by Google that performs smarter static code analysis. Apart from being a great standalone mistake finder, it also integrates with other projects such as NullAway , 開発者が見つけて修正するのを助けるツールNullPointerException
Javaコードのs.プラグインをあなたの
settings.gradle.kts
ファイルpluginManagement {
plugins {
id("net.ltgt.errorprone").version("2.0.2")
}
}
次に、次の設定をサブプロジェクトのbuild.gradle.kts
ファイルimport net.ltgt.gradle.errorprone.errorprone
plugins {
id("net.ltgt.errorprone")
}
dependencies {
errorprone(libs.errorprone.core)
errorprone(libs.nullaway)
}
// hook up the checker to the compilation target
tasks.withType<JavaCompile>().configureEach {
options.errorprone.disableWarningsInGeneratedCode.set(true)
}
Conclusion
Hopefully, this tutorial gave you an idea of the first steps you should take towards starting an open-source Java library on GitHub.
So far, I only spent a few hours learning how to do all of these from scratch and had a lot of fun doing it!
Since I haven't ported over any code from the v1 repo, I have probably yet to smooth out all the rough edges. If you end up using it, ping me and tell me how you got on! ( I'd appreciate it! )
You can find all the code referenced above on GitHub in the props project !滞在は、このシリーズでより多くの記事のためにチューニングし、忘れないでくださいsubscribe to my newsletter ! 私はより多くのコードを書くように定期的な更新を送信します.
ありがとうございます!
閉じるこの動画はお気に入りから削除されています.please subscribe to my newsletter ; 私は数週間ごとに1つを送信!
Reference
この問題について(Gradleビルドツールを使用した新しいオープンソースJavaプロジェクトの作成), 我々は、より多くの情報をここで見つけました https://dev.to/mihaibojin/create-a-new-open-source-java-project-using-the-gradle-build-tool-5abjテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol