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

Unable to 'edit starters' in child project #110

Closed
shankarramr opened this issue Jan 23, 2019 · 6 comments
Closed

Unable to 'edit starters' in child project #110

shankarramr opened this issue Jan 23, 2019 · 6 comments
Milestone

Comments

@shankarramr
Copy link

shankarramr commented Jan 23, 2019

I am unable to use the 'edit starters' feature from inside the pom of the child project.

Steps:
Created a spring boot project via Spring Initializr
Opened pom -> right click -> Edit starters

Error:
Fail to edit starters. Error: Not a valid Spring Boot project.

VScode version: 1.31.0-insider
Spring Initializr version: 0.4.4

Note: I am able to do this from the parent project pom, but it does not work from the child project pom

@shankarramr shankarramr changed the title Unable to 'edit starters' Unable to 'edit starters' in child project Jan 23, 2019
@Eskibear Eskibear added the bug label Jan 23, 2019
@Eskibear
Copy link
Member

Current logic to judge a valid spring-boot project is simple and naive. It simply parse the pom file, and the condition is:

parent.groupId == "org.springframework.boot" && parent.artifactId == "spring-boot-starter-parent"

Any simple and effective way to identify a spring-boot project?

@Eskibear Eskibear added this to the Backlog milestone Jan 23, 2019
@Eskibear
Copy link
Member

I'm thinking about the right behavior when editing starters for a child project. If some starters are inherited from its parent, should we be able to edit them?

e.g.

parent

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>yanzh</groupId>
  <artifactId>parent</artifactId>
  <version>0.1</version>
  <packaging>pom</packaging>
  <name>parent</name>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
  </parent>
  <dependencies>
    <!-- web stater -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

child

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>yanzh</groupId>
    <artifactId>parent</artifactId>
    <version>0.1</version>
    <relativePath>../</relativePath>
  </parent>
  <groupId>yanzh</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>demo</name>
  <description>Demo project for Spring Boot</description>
  <properties>
    <java.version>1.8</java.version>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

@Eskibear
Copy link
Member

Eskibear commented Mar 8, 2019

The fix has been released in v0.4.5.

@luangong
Copy link

luangong commented May 9, 2022

@Eskibear I’m still having this issue. Pull request #117 didn’t handle the case where the <relativePath> element is omitted, which indicates that the parent POM is the pom.xml in the parent folder (../pom.xml). This is very common in a multi-module setup.

Currently, the workaround is to explicitly add <relativePath>../pom.xml</relativePath> to the submodule. However, I’m working on a multi-module project with 75 submodules, all of which are Spring Boot projects, and adding the <relativePath> element 75 times is rather tedious.

B.T.W., it would be great if you can also add some unit tests alongside the code (I’m surprised to see barely any unit tests in this codebase), because there is another case where the relative path is an empty string (<relativePath />), which indicates that the parent POM should always be searched in the repository instead of the project folder.

References:

@Eskibear
Copy link
Member

Eskibear commented May 9, 2022

@luangong Good catch. We can fallback to "../pom.xml" when nothing is read from relativePath node. I assume this small change would be a quick fix.

i.e.

return getNode(parentNode, "relativePath");

change this line to

 return getNode(parentNode, "relativePath", "../pom.xml");

@luangong
Copy link

luangong commented May 9, 2022

@Eskibear Thanks for the quick reply. That line should do the trick. Just don’t forget to handle the case where the <relativePath> element is present but contains no content (<relativePath />), in here:

const relativePath = getParentRelativePath(projectNode);
if (relativePath) {
const newPath = path.join(path.dirname(uri.path), relativePath);
let newUri = uri.with({path: newPath});
if (await isDirectory(newUri)) {
newUri = uri.with({path: path.join(newPath, "pom.xml")});
}
if (await pathExists(newUri)) {
return await searchForBootVersion(newUri);
}
}
return undefined;

Add an else if block like this:

const relativePath = getParentRelativePath(projectNode);
if (relativePath) {
  // Get Spring Boot version. Code omitted for brevity.
} else if (relativePath === '') {
  // TODO: Continue search into the repository
}
return undefined;

The reason is that there may the case where someone creates a Spring Boot project (packaging: pom) and publish it to the company’s internal Maven repository so that it can be used as the parent project of all other projects for the company.

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

No branches or pull requests

3 participants