Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use ktlint for nx-maven and nx-gradle? #804

Open
dfiai opened this issue Jan 25, 2024 · 6 comments
Open

How to use ktlint for nx-maven and nx-gradle? #804

dfiai opened this issue Jan 25, 2024 · 6 comments

Comments

@dfiai
Copy link

dfiai commented Jan 25, 2024

What alternative should be used for linting in this case?

@dfiai dfiai changed the title Disabling the nx-ktlint npm packages resulted in a failure during the npm i command. Disabling the nx-ktlint npm package resulted in a failure during the npm i command. Jan 25, 2024
@khalilou88
Copy link
Owner

khalilou88 commented Jan 25, 2024

Hi @dfiai, the alternative is maven and gradle plugins with run-task executor. check this repo for examples : https://github.com/khalilou88/jnxplus-examples

@khalilou88
Copy link
Owner

For nx-maven:
1- add plugin to root pom.xml or a parent-project:

   <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>3.1.0</version>
        <executions>
          <execution>
            <id>ktlint</id>
            <configuration>
              <target name="ktlint">
                <java
                  taskname="ktlint"
                  dir="${basedir}"
                  fork="true"
                  failonerror="true"
                  classpathref="maven.plugin.classpath"
                  classname="com.pinterest.ktlint.Main"
                >
                  <jvmarg value="--add-opens=java.base/java.lang=ALL-UNNAMED" />
                  <!-- see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information -->
                  <arg value="src/**/*.kt" />
                </java>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
          <execution>
            <id>ktlint-format</id>
            <configuration>
              <target name="ktlint">
                <java
                  taskname="ktlint"
                  dir="${basedir}"
                  fork="true"
                  failonerror="true"
                  classpathref="maven.plugin.classpath"
                  classname="com.pinterest.ktlint.Main"
                >
                  <jvmarg value="--add-opens=java.base/java.lang=ALL-UNNAMED" />
                  <!-- see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information -->
                  <arg value="-F" />
                  <arg value="src/**/*.kt" />
                </java>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>com.pinterest.ktlint</groupId>
            <artifactId>ktlint-cli</artifactId>
            <version>1.1.0</version>
          </dependency>
        </dependencies>
      </plugin>

2- add executors to project.json:

    "ktlint": {
      "executor": "@jnxplus/nx-maven:run-task",
      "options": {
        "task": "antrun:run@ktlint"
      }
    },
    "ktlint-format": {
      "executor": "@jnxplus/nx-maven:run-task",
      "options": {
        "task": "antrun:run@ktlint-format"
      }
    }

@dfiai
Copy link
Author

dfiai commented Jan 25, 2024

@khalilou88 thank you! I'm using gradle, do you have an example for it?

@khalilou88
Copy link
Owner

khalilou88 commented Jan 25, 2024

For gradle:
1- add plugin https://github.com/JLLeitschuh/ktlint-gradle to build.gradle or build.gradle.kts

alias(libs.plugins.jlleitschuh.gradle.ktlint)

2- add executors to project.json:

    "ktlint": {
      "executor": "@jnxplus/nx-gradle:run-task",
      "options": {
        "task": "ktlintCheck"
      }
    },
    "ktlint-format": {
      "executor": "@jnxplus/nx-gradle:run-task",
      "options": {
        "task": "ktlintFormat"
      }
    }

@dfiai
Copy link
Author

dfiai commented Jan 25, 2024

thank you!

@khalilou88
Copy link
Owner

khalilou88 commented Jan 25, 2024

I am not planning to add ktlint to nx-maven or nx-gradle to keep the plugins simple but I will add this section to docs

@khalilou88 khalilou88 changed the title Disabling the nx-ktlint npm package resulted in a failure during the npm i command. How to use ktlint for nx-maven and nx-gradle? Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants