I want to ESLint in Maven project. I want to run Node.js without putting it in my environment.

Introduction

I was in trouble because I didn't think that there was a plug-in if it was as famous as ESLint, so I solved it. I don't want to install node by myself due to version issues, so if I'm using Maven in a Java project, I thought about how to install a plugin, so it looks like this article.

environment

Adopt Open JDK Hotspot 11.0.3 Maven 3.6.0

Project creation

I will omit it here. Any Maven project is fine.

Create package.json

I will do npm install in Maven, so I will make it first.

package.json


{
  "dependencies": {},
  "devDependencies": {
    "eslint": "^6.5.1"
  }
}

For the time being, it looks like this at the minimum. Since there are many releases, please read the version as appropriate.

Let's put node first

Since it can be installed with a plugin called frontend-maven-plugin, we will set it first. Basically, you can set it as officially.

pom.xml


            <plugin>
                <groupId>com.github.eirslett</groupId>
                <artifactId>frontend-maven-plugin</artifactId>
                <version>1.8.0</version>
                <configuration>
                    <nodeVersion>v10.16.3</nodeVersion>
                </configuration>
                <executions>
                    <execution>
                        <id>install node and npm</id>
                        <goals>
                            <goal>install-node-and-npm</goal>
                        </goals>
                        <phase>generate-resources</phase>
                    </execution>
                    <execution>
                    <id>npm install</id>
                    <goals>
                        <goal>npm</goal>
                    </goals>
                    <phase>generate-resources</phase>
                    <configuration>
                        <arguments>install</arguments>
                    </configuration>
                </execution>
                </executions>
            </plugin>

Start for the time being

./mvnw generate-resources will create node related files.

Initialize Eslint (skip if you have the necessary files)

If you don't have the file to run ESlint, you need to initialize it here. However, just note that the node executable is in the node folder of the project root, so you have to add it to your PATH.

export PATH=node/:$PATH

All you have to do is run it and set it up. I was thinking of setting it from Gitbash on Windows first, but it didn't work, so I recommend doing it on a Mac.

node_modules/.bin/eslint --init

Creating eslint.json

Since I want to set the same rule on the Eclipse side in the story of the project under development in Eclipse, I have separated the inited hidden file with a symbolic link. It seems that you can only use Tern to do it in Eclipse, but it is a little convenient because you can follow the same rules.

I feel sad to develop Javascript using Eclipse, so I personally want to use VS Code only when playing with Javascript, but this time I can do all development on Eclipse for the convenience of the project. We are implementing this kind of response because we have to do so.

ESLint execution settings

Now that we are ready, we will play with pom.xml again so that ESLint can be executed from Maven. This is done using ʻexec-maven-plugin. I wish I could do it with frontend-maven-plugin`, but it didn't work so I'm trying to use another plugin.

Here, it is set to call eslint_exec.sh from the bash command.

pom.xml


            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>verify</phase>
                        <id>exec-eslint</id>
                        <configuration>
                            <executable>bash</executable>
                            <arguments>
                                <argument>eslint_exec.sh</argument>
                            </arguments>
                        </configuration>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

The contents of eslint_exec.sh look like this. Setting under node in PATH is troublesome to have it set in various environments, so I decided to execute it in this. The rest is just running ESLint. ʻExit 0` is included because I didn't want to make an error even if I got caught in the check when running with Jenkins.

eslint_exec.sh


export PATH=node/:$PATH
node_modules/.bin/eslint src/main/resources/static/js/*/**
exit 0

Run ESLint

Since verify will do what you want to do this time, set the goal setting to verify.

./mvnw verify

in conclusion

I don't think it's possible to develop Javascript in this age, so I thought it would have been better to think of a way to build it a little easier overall.

Recommended Posts

I want to ESLint in Maven project. I want to run Node.js without putting it in my environment.
I want to display the images under assets/images in the production environment
My memorandum that I want to make ValidationMessages.properties UTF8 in Spring Boot
[Java Spring MVC] I want to use DI in my own class
I want to use @Autowired in Servlet
Run R from Java I want to run rJava
I want to send an email in Java.
I want to use arrow notation in Ruby
Run JUnit and Spock in a maven project
I made a Docker container to run Maven
rsync4j --I want to touch rsync in Java.
[Xcode] I want to manage images in folders
I want to be eventually even in kotlin
I want to get the value in Ruby
I want to morphologically analyze the log in the DB and put it in the DB to classify messages 1
[Rails] How to delete production environment MySQL data after putting it in the development environment
Docker command to create Rails project with a single blow in environment without Ruby
First AWS Lambda (I tried to see what kind of environment it works in)
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
I want to do something like "cls" in Java
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to use a little icon in Rails
I want to define a function in Rails Console
[Android Studio] I want to use Maven library on Android
I want to stop snake case in table definition
I want to click a GoogleMap pin in RSpec
I installed Squid on CentOS in my local environment
After posting an article with Rails Simple Calendar, I want to reflect it in the calendar.
I want to recreate the contents of assets from scratch in the environment built with capistrano