• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 20115895339

10 Dec 2025 10:55PM UTC coverage: 70.14% (+0.2%) from 69.924%
20115895339

push

github

web-flow
#1166 automatic project import for intellij #1508: fix xml merge when file is empty (#1649)

Co-authored-by: cthies <caroline.thies@capgemini.com>
Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>
Co-authored-by: jan-vcapgemini <jan-vincent.hoelzle@capgemini.com>

3963 of 6211 branches covered (63.81%)

Branch coverage included in aggregate %.

10138 of 13893 relevant lines covered (72.97%)

3.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.14
cli/src/main/java/com/devonfw/tools/ide/tool/intellij/Intellij.java
1
package com.devonfw.tools.ide.tool.intellij;
2

3
import java.nio.file.Files;
4
import java.nio.file.Path;
5
import java.util.Map;
6
import java.util.Map.Entry;
7
import java.util.Set;
8

9
import org.w3c.dom.Document;
10

11
import com.devonfw.tools.ide.cli.CliException;
12
import com.devonfw.tools.ide.commandlet.CommandletManager;
13
import com.devonfw.tools.ide.common.Tag;
14
import com.devonfw.tools.ide.context.IdeContext;
15
import com.devonfw.tools.ide.environment.AbstractEnvironmentVariables;
16
import com.devonfw.tools.ide.environment.EnvironmentVariables;
17
import com.devonfw.tools.ide.environment.ExtensibleEnvironmentVariables;
18
import com.devonfw.tools.ide.merge.xml.XmlMergeDocument;
19
import com.devonfw.tools.ide.merge.xml.XmlMerger;
20
import com.devonfw.tools.ide.process.EnvironmentContext;
21
import com.devonfw.tools.ide.tool.LocalToolCommandlet;
22
import com.devonfw.tools.ide.tool.ToolInstallation;
23
import com.devonfw.tools.ide.tool.gradle.Gradle;
24
import com.devonfw.tools.ide.tool.ide.IdeToolCommandlet;
25
import com.devonfw.tools.ide.tool.ide.IdeaBasedIdeToolCommandlet;
26
import com.devonfw.tools.ide.tool.mvn.Mvn;
27

28
/**
29
 * {@link IdeToolCommandlet} for <a href="https://www.jetbrains.com/idea/">IntelliJ</a>.
30
 */
31
public class Intellij extends IdeaBasedIdeToolCommandlet {
32

33
  private static final String IDEA = "idea";
34

35
  private static final String IDEA64_EXE = IDEA + "64.exe";
36

37
  private static final String IDEA_BASH_SCRIPT = IDEA + ".sh";
38

39
  private static final String FOLDER_IDEA_CONFIG = ".idea";
40
  private static final String TEMPLATE_LOCATION = "intellij/workspace/repository/" + FOLDER_IDEA_CONFIG;
41
  private static final String GRADLE_XML = "gradle.xml";
42
  private static final String MISC_XML = "misc.xml";
43
  private static final String IDEA_PROPERTIES = "idea.properties";
44

45
  private static final Map<Class<? extends LocalToolCommandlet>, String> BUILD_TOOL_TO_IJ_TEMPLATE = Map.of(Mvn.class, MISC_XML, Gradle.class, GRADLE_XML);
7✔
46

47
  /**
48
   * The constructor.
49
   *
50
   * @param context the {@link IdeContext}.
51
   */
52
  public Intellij(IdeContext context) {
53

54
    super(context, "intellij", Set.of(Tag.INTELLIJ));
6✔
55
  }
1✔
56

57
  @Override
58
  protected String getBinaryName() {
59

60
    if (this.context.getSystemInfo().isWindows()) {
5✔
61
      return IDEA64_EXE;
2✔
62
    } else {
63
      if (Files.exists(this.getToolBinPath().resolve(IDEA))) {
8✔
64
        return IDEA;
2✔
65
      } else if (Files.exists(this.getToolBinPath().resolve(IDEA_BASH_SCRIPT))) {
8✔
66
        return IDEA_BASH_SCRIPT;
2✔
67
      } else {
68
        return IDEA;
2✔
69
      }
70
    }
71
  }
72

73
  @Override
74
  public void setEnvironment(EnvironmentContext environmentContext, ToolInstallation toolInstallation, boolean additionalInstallation) {
75
    super.setEnvironment(environmentContext, toolInstallation, additionalInstallation);
5✔
76
    environmentContext.withEnvVar("IDEA_PROPERTIES", this.context.getWorkspacePath().resolve(IDEA_PROPERTIES).toString());
10✔
77
  }
1✔
78

79
  private EnvironmentVariables getIntellijEnvironmentVariables(Path projectPath) {
80
    ExtensibleEnvironmentVariables environmentVariables = new ExtensibleEnvironmentVariables(
4✔
81
        (AbstractEnvironmentVariables) this.context.getVariables().getParent(), this.context);
7✔
82

83
    environmentVariables.setValue("PROJECT_PATH", projectPath.toString().replace('\\', '/'));
8✔
84
    return environmentVariables.resolved();
3✔
85
  }
86

87
  private void mergeConfig(Path repositoryPath, String configFilePath) {
88
    Path templatePath = this.context.getSettingsPath().resolve(TEMPLATE_LOCATION);
6✔
89
    Path templateFile = templatePath.resolve(configFilePath);
4✔
90
    if (!Files.exists(templateFile)) {
5!
91
      throw new CliException(
×
92
          "Cannot import project into workspace: template file not found at " + templateFile + "\n"
93
              + "Please do an upstream merge of your settings git repository.");
94
    }
95
    Path workspacesPath = this.context.getIdeHome().resolve(IdeContext.FOLDER_WORKSPACES);
6✔
96
    Path workspacePath = this.context.getFileAccess().findAncestor(repositoryPath, workspacesPath, 1);
8✔
97
    if (workspacePath == null) {
2!
98
      throw new CliException(
×
99
          "Cannot import project into workspace: could not find workspace from " + repositoryPath);
100
    }
101
    XmlMerger xmlMerger = new XmlMerger(this.context);
6✔
102
    EnvironmentVariables environmentVariables = getIntellijEnvironmentVariables(workspacePath.relativize(repositoryPath));
6✔
103
    Path workspaceFile = workspacePath.resolve(FOLDER_IDEA_CONFIG).resolve(configFilePath);
6✔
104

105
    XmlMergeDocument workspaceDocument = xmlMerger.load(workspaceFile);
4✔
106
    XmlMergeDocument templateDocument = xmlMerger.loadAndResolve(templateFile, environmentVariables);
5✔
107

108
    Document mergedDocument = xmlMerger.merge(templateDocument, workspaceDocument, false);
6✔
109

110
    xmlMerger.save(mergedDocument, workspaceFile);
4✔
111
  }
1✔
112

113
  @Override
114
  public void importRepository(Path repositoryPath) {
115
    CommandletManager commandletManager = this.context.getCommandletManager();
4✔
116
    for (Entry<Class<? extends LocalToolCommandlet>, String> entry : BUILD_TOOL_TO_IJ_TEMPLATE.entrySet()) {
11!
117
      LocalToolCommandlet buildTool = commandletManager.getCommandlet(entry.getKey());
7✔
118
      Path buildDescriptor = buildTool.findBuildDescriptor(repositoryPath);
4✔
119
      if (buildDescriptor != null) {
2✔
120
        String templateFilename = entry.getValue();
4✔
121
        this.context.debug("Found build descriptor {} so merging template {}", buildDescriptor, templateFilename);
14✔
122
        mergeConfig(repositoryPath, templateFilename);
4✔
123
        return;
1✔
124
      }
125
    }
1✔
126
    this.context.warning("No supported build descriptor was found for project import in {}", repositoryPath);
×
127
  }
×
128

129
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc