Skip to content

Add Gradle tasks and Git hooks for IntelliJ-based code formatting (#15535)#15555

Open
sanjana2505006 wants to merge 1 commit intoapache:7.0.xfrom
sanjana2505006:feat/intellij-formatter-15535
Open

Add Gradle tasks and Git hooks for IntelliJ-based code formatting (#15535)#15555
sanjana2505006 wants to merge 1 commit intoapache:7.0.xfrom
sanjana2505006:feat/intellij-formatter-15535

Conversation

@sanjana2505006
Copy link
Copy Markdown
Contributor

@sanjana2505006 sanjana2505006 commented Apr 4, 2026

Added a formatCode Gradle task that uses the IntelliJ CLI formatter to keep our code style consistent. I also added an installGitHooks task that sets up a git pre-commit hook to automatically format staged files. This should help avoid formatting issues during review. Fixes #15535.

@sanjana2505006 sanjana2505006 force-pushed the feat/intellij-formatter-15535 branch from 56037b6 to e886bca Compare April 4, 2026 19:09
@jdaugherty
Copy link
Copy Markdown
Contributor

@sanjana2505006 we explicitly decided to not auto-install the hook so it would be optional if people wanted it.

.map(Long::parseLong)
.map(Instant::ofEpochSecond)
.orElseGet(Instant::now)
.filter { s -> !s.isEmpty() }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please revert these changes.

@bito-code-review
Copy link
Copy Markdown

To revert the changes in build.gradle, remove the added plugin block and revert the lambda expressions in the ext block back to method references.

build.gradle

/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */

import java.time.Instant
import java.time.LocalDate
import java.time.ZoneOffset
import java.time.format.DateTimeFormatter
import org.apache.tools.ant.taskdefs.condition.Os
ext {
    isReproducibleBuild = System.getenv("SOURCE_DATE_EPOCH") != null
    buildInstant = java.util.Optional.ofNullable(System.getenv("SOURCE_DATE_EPOCH"))
            .filter(s -> !s.isEmpty())
            .map(Long::parseLong)
            .map(Instant::ofEpochSecond)
            .orElseGet(Instant::now)

private static void doNotApplyStylingToTests(Project project) {
project.tasks.named('checkstyleTest') {
it.enabled = false // Do not check test sources at this time
if (project.tasks.names.contains('checkstyleTest')) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me understand why this change was necessary?

}
}

private static void registerFormattingTasks(Project project) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code style work is planned to move to it's own repository (eventually). I'm ok with putting the formatting logic adjacent to this plugin, but I don't think it should be in this plugin since it's effectively a root only plugin - the root project won't ever have sources. Let's move this to it's own format plugin?

formatExec = new File(ideaHome, "bin/$executable")
} else {
// Try common paths on macOS
if (Os.isFamily(Os.FAMILY_MAC)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just make it a requirement that idea is on the path and reference the version on the path. IntelliJ has an option to install the command line tools (https://www.jetbrains.com/help/idea/working-with-the-ide-features-from-command-line.html) so let's just reference the help article and not trying to be smart about locating it (anyone using JetBrains toolbox won't be installing it to /Applications or to a specific location)

}

if (formatExec == null || !formatExec.exists()) {
project.logger.error("IntelliJ formatter executable not found.")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's deprecated to reference project from inside of a task. logger should be available already.

throw new RuntimeException("IntelliJ formatter executable not found.")
}

def filesToFormat = project.findProperty('formatFiles')
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hadn't considered the useful of having a format task added to the subprojects. I still think we should split the format tasks out from the code style though - otherwise you're trying to apply styling to the root project which we don't want to do.

throw new RuntimeException("IntelliJ code style settings not found at ${settingsFile.absolutePath}")
}

project.exec { ExecSpec exec ->
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at the publishAllToMavenLocal task for how to do in this a modern Gradle way without referencing project .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Add gradle tasks to assist in code formatting

2 participants