[Note] A story about changing Java build tools with VS Code

Introduction

I was coding Java in VS Code, and I was really into solving Dependency. As a memorandum (probably I'm addicted to it again), I will describe the process of the investigation.

It happened after I tried to modify the Spring Boot sample code that I cloned from GitHub, and rewrote pom.xml in good condition.

"The added dependency is not reflected in VS Code ...?"

From there, the investigation began.

Investigation

Java Extension Pack is the only Java-related extension installed in VS Code, so if you have a problem with Java Extension Pack, you should be able to find out by asking the public (* Google).

However, even when I hear the opinions of the public (* Stack Overflow), when I update pom.xml, a confirmation dialog like "Do you want to reflect the update?" Appears and it is reflected properly.

The only possibility was that there was a problem with the Java Language Server cache and that it should be cleared.

let's do it.

Action 1 (failure)

The VS Code environment in question is running on Ubuntu with WLS2 using Remote-WLS. VS Code cached data is located in the following location:

~/.vscode-server/data/User/workspaceStorage/

Exit VS Code once, access the Ubuntu terminal from Windows Terminal, and delete the cached data. Then restart VS Code. If it's a cache problem, this is the solution. To be honest, I thought I won. Well, if that works, we're not addicted to the behavior of the tool, and I wouldn't write an article like this, so that thought was just an illusion.

Not surprisingly, the dependencies haven't been updated on the restarted VS Code. I tried mvn compile in vain, but it didn't respond at all. When I thought it was a stalemate, I suddenly remembered something. Yes, I don't remember seeing the example dialog that should be displayed when I edited pom.xml.

The thought of wondering runs through my head. This sample code can be built not only with maven but also with gradle. I'm not usually a gradle sect, so bundle.gradle, which I pretended to be completely unseen, is also included in this sample code ... in the same hierarchy as pom.xml.

.classpath


<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="bin/main" path="src/main/java">
        <attributes>
            <attribute name="gradle_scope" value="main"/>
            <attribute name="gradle_used_by_scope" value="main,test"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="bin/test" path="src/test/java">
        <attributes>
            <attribute name="gradle_scope" value="test"/>
            <attribute name="gradle_used_by_scope" value="test"/>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="bin/main" path="src/main/resources">
        <attributes>
            <attribute name="gradle_scope" value="main"/>
            <attribute name="gradle_used_by_scope" value="main,test"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
    <classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
    <classpathentry kind="output" path="bin/default"/>
</classpath>

Unintentionally, I became half laughed. Actually, I've been addicted to this problem for less than two hours. It's a painful blow. There is no place to hit this unfocused feeling. This .classpath is a file that specifies the directories and libraries to be included in the target for dependency resolution on the VS Code side, and is automatically generated. Well, no matter how much you rewrite maven's pom.xml, the dependency will not be reflected. It wasn't referenced from the beginning ... haha (´ ・ ω ・ `)

There are two options for solving this phenomenon. ・ Switch the build tool to gradle obediently. -Forcibly regenerate .classpath so that the dependency is referenced by maven.

Action 2 (Failure again)

It was a tantrum that fell on gradle's army gate, so I chose the latter without hesitation. I quit VS Code and mved .classpath and build.gradle from the terminal. Now when you reboot, a .classpath will not be created based on maven ... right? Hmm?

I was a little worried, but the culprit was .project.

.project


<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>spring-boot</name>
    <comment>Project complete created by Buildship.</comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.buildship.core.gradleprojectbuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.buildship.core.gradleprojectnature</nature>
    </natures>
</projectDescription>

It says gradle to the fullest. Let's mv this too. Restart VS Code. This time, the dependencies are properly reflected based on maven. However, .project and .classpath are not generated ... It's okay to code, but it's very unpleasant. It can also be a cause of addiction.

Also, I'm wondering when .project and .classpath are generated. Let's verify.

Verification

Create another directory and clone it again from the original GitHub repository. When I opened it with VS Code ... it was generated.

This is the state of the target directory that has just been cloned image.png

This is the state after starting VS Code. At this point a .project was already generated and the inside was written to use gradle. There is also a .classpath. As a matter of course, I started to use gradle. Apparently, gradle has priority over maven.

image.png

The following files and directories have been added. ・ .Classpath ・ .Gradle ・ .Project ・ .Settings ・ Bin In other words, when VS Code opens the directory, (probably) chooses to use build.gradle as a build tool, generates a .project file, and the project environment is automatic based on gradle. I think that it is set as a target. Now, let's clone this project again and check the behavior when build.gradle is deleted. The directory before opening VS Code looks like this.

image.png

When I opened it in VS Code, both .project and .classpath were generated.

.project


<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
    <name>spring-boot</name>
    <comment></comment>
    <projects>
    </projects>
    <buildSpec>
        <buildCommand>
            <name>org.eclipse.jdt.core.javabuilder</name>
            <arguments>
            </arguments>
        </buildCommand>
        <buildCommand>
            <name>org.eclipse.m2e.core.maven2Builder</name>
            <arguments>
            </arguments>
        </buildCommand>
    </buildSpec>
    <natures>
        <nature>org.eclipse.jdt.core.javanature</nature>
        <nature>org.eclipse.m2e.core.maven2Nature</nature>
    </natures>
</projectDescription>

.classpath


<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" output="target/classes" path="src/main/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/test-classes" path="src/test/java">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
        <attributes>
            <attribute name="maven.pomderived" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" path="target/generated-sources/annotations">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="ignore_optional_problems" value="true"/>
            <attribute name="m2e-apt" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
        <attributes>
            <attribute name="optional" value="true"/>
            <attribute name="maven.pomderived" value="true"/>
            <attribute name="ignore_optional_problems" value="true"/>
            <attribute name="m2e-apt" value="true"/>
            <attribute name="test" value="true"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="output" path="target/classes"/>
</classpath>

Both have been replaced by maven base. This is my goal.

Action 3 (success)

So what wasn't enough to re-recognize what was once recognized as a gradle project in VS Code as a maven project? I mved build.gradle, .classpath, and .project from the gradle project earlier, but it also contains auto-generated directories and gradle related resources. But what is suspicious is the automatically generated resource. Therefore, let's mv them all. ... that won't change, right?

So I came with a pin. The cause was that I mved .project and .classpath to a directory called backup under the project root. Apparently, there is a specification that even if .project is not under the root directory, it will be read if it exists in a subdirectory. By deleting the .project and .classpath, a new .project and .classpath was generated and the project build tool was switched to maven.

Summary

--In VS Code's Java Extension Pack, the first time you open the directory where pom.xml or build.gradle exists, a .project will be generated and recognized as a Java project. --If both build.gradle and pom.xml are present, gradle takes precedence. --Once the build tool has been decided, there is no way to change it on the VS Code side. (Therefore, I'm addicted to doing stupid things such as modifying only pom.xml) --If you want to migrate the build tool, you need to delete .project and .classpath and open the directory again with VS Code in addition to build.gradle. --mv and remove are different.

It is that. I made a detour a lot, but I was able to successfully switch the build tool to maven.

Congratulations, congratulations.

Toppin Parari no Puu.

Recommended Posts

[Note] A story about changing Java build tools with VS Code
Build a Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Java build with mac vs code
Using Gradle with VS Code, build Java → run
Try debugging a Java program with VS Code
Build Java development environment with VS Code on Mac
Build Java development environment with WSL2 Docker VS Code
How to build Java development environment with VS Code
A story about developing ROS called rosjava with java
A memo to start Java programming with VS Code (2020-04 version)
Build a Java project with Gradle
A story about hitting the League Of Legends API with JAVA
A story about having a hard time aligning a testing framework with Java 6
Prepare Java development environment with VS Code
[Be careful about changing the version of Xdebug! ] Create a development environment with Xdebug3 + docker + VS Code
About build tools
A story about Java 11 support for Web services
A story about the JDK in the Java 11 era
A story about trying to operate JAVA File
[PHP] A story about outputting PDF with TCPDF + FPDI
Build VS Code + WSL + Java + Gradle environment from scratch
Lombok with VS Code
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Build ruby debug environment with VS Code of Windows 10
java build a triangle
A story about trying to get along with Mockito
[Note] Build a Python3 environment with Docker in EC2
[Note] Create a java environment from scratch with docker
A story about reducing memory consumption to 1/100 with find_in_batches
Build Java with Wercker
Build Java program development environment with Visual Studio Code
Create a Java (Maven) project with VS Code and develop it on a Docker container
Beginners create Spring Tools Suite environment with VS Code
Java web application development environment construction with VS Code (struts2)
[Java] A story about IntelliJ IDEA teaching Map's putIfAbsent method
About the behavior when doing a file map with java
Talk about using Java input wait (Scanner) in VS Code
A confused story about a ternary operator with multiple conditional expressions
Build WebAPP development environment with Java + Spring with Visual Studio Code
A story about misunderstanding how to use java scanner (memo)
Docker management with VS Code
A note about the scope
Format Ruby with VS Code
Hello World with VS Code!
Create a VS Code Plugin.
A private note about AtomicReference
Study Java with Progate Note 1
A story stuck with NotSerializableException
A story that I struggled to challenge a competition professional with Java
Settings to delete unused Java imports when saving with VS Code
Building a haskell environment with Docker + VS Code on Windows 10 Home
How to open a script file from Ubuntu with VS code
[Gradle] Build a Java project with a configuration different from the convention
A story about a major SIer engineer who can't write code properly created a blockbuster Android app with 600,000 downloads
A note about RocksDB's Column Families
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Code Java from Emacs with Eclim
[Java] Points to note with Arrays.asList ()
Learn about transaction savepoints (with Java)