khalilou88 / jnxplus

Nx plugins that adds Java/Kotlin monorepo support to Nx workspace using Gradle and Maven
MIT License
63 stars 14 forks source link

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

Open dfiai opened 7 months ago

dfiai commented 7 months ago

What alternative should be used for linting in this case?

khalilou88 commented 7 months ago

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 commented 7 months ago

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 commented 7 months ago

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

khalilou88 commented 7 months ago

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 commented 7 months ago

thank you!

khalilou88 commented 7 months ago

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