IDEA-84573 (Maven: configurable generate-sources behavior - use it as a source root or use intermediate folders)

This commit is contained in:
Sergey Evdokimov
2012-05-11 17:17:31 +04:00
parent 5f0d946931
commit 8597fb4924
5 changed files with 157 additions and 16 deletions
@@ -15,6 +15,8 @@
*/
package org.jetbrains.idea.maven.importing;
import com.intellij.ide.util.projectWizard.importSources.JavaModuleSourceRoot;
import com.intellij.ide.util.projectWizard.importSources.JavaSourceRootDetectionUtil;
import com.intellij.openapi.application.AccessToken;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.module.Module;
@@ -23,6 +25,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.Pair;
import com.intellij.util.ArrayUtil;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.model.MavenResource;
@@ -33,6 +36,7 @@ import org.jetbrains.idea.maven.utils.Path;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
@@ -169,7 +173,7 @@ public class MavenFoldersImporter {
Boolean isGeneratedTestSources = generatedDirs.get(f);
if (isGeneratedTestSources != null) {
addAllSubDirsAsSources(f, isGeneratedTestSources);
configGeneratedSourceFolder(f, isGeneratedTestSources);
}
else {
if (myModel.hasRegisteredSourceSubfolder(f)) continue;
@@ -195,7 +199,35 @@ public class MavenFoldersImporter {
}
}
private void addAllSubDirsAsSources(File dir, boolean isTestSources) {
private void configGeneratedSourceFolder(@NotNull File dir, boolean isTestSources) {
switch (myImportingSettings.getGeneratedSourcesFolder()) {
case GENERATED_SOURCE_FOLDER:
if (!myModel.hasRegisteredSourceSubfolder(dir)) {
myModel.addSourceFolder(dir.getPath(), isTestSources);
}
break;
case SUBFOLDER:
addAllSubDirsAsSources(dir, isTestSources);
break;
case AUTODETECT:
Collection<JavaModuleSourceRoot> sourceRoots = JavaSourceRootDetectionUtil.suggestRoots(dir);
if (sourceRoots.size() == 1) {
JavaModuleSourceRoot root = sourceRoots.iterator().next();
if (dir.equals(root.getDirectory())) {
if (!myModel.hasRegisteredSourceSubfolder(dir)) {
myModel.addSourceFolder(dir.getPath(), isTestSources);
}
break;
}
}
addAllSubDirsAsSources(dir, isTestSources);
break;
}
}
private void addAllSubDirsAsSources(@NotNull File dir, boolean isTestSources) {
for (File f : getChildren(dir)) {
if (!f.isDirectory()) continue;
if (myModel.hasRegisteredSourceSubfolder(f)) continue;
@@ -203,8 +235,8 @@ public class MavenFoldersImporter {
}
}
private File[] getChildren(File dir) {
private static File[] getChildren(File dir) {
File[] result = dir.listFiles();
return result == null ? new File[0] : result;
return result == null ? ArrayUtil.EMPTY_FILE_ARRAY : result;
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.idea.maven.project;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.annotations.Property;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -46,8 +47,22 @@ public class MavenImportingSettings implements Cloneable {
private boolean downloadSourcesAutomatically = false;
private boolean downloadDocsAutomatically = false;
private GeneratedSourcesFolder generatedSourcesFolder = GeneratedSourcesFolder.AUTODETECT;
private List<Listener> myListeners = ContainerUtil.createEmptyCOWList();
public enum GeneratedSourcesFolder {
AUTODETECT("Auto detect"),
GENERATED_SOURCE_FOLDER("target/generated-sources"),
SUBFOLDER("subdirectories of \"target/generated-sources\"");
public final String title;
private GeneratedSourcesFolder(String title) {
this.title = title;
}
}
@NotNull
public String getDedicatedModuleDir() {
return dedicatedModuleDir;
@@ -132,6 +147,18 @@ public class MavenImportingSettings implements Cloneable {
this.downloadDocsAutomatically = value;
}
@Property
@NotNull
public GeneratedSourcesFolder getGeneratedSourcesFolder() {
return generatedSourcesFolder;
}
public void setGeneratedSourcesFolder(GeneratedSourcesFolder generatedSourcesFolder) {
if (generatedSourcesFolder == null) return; // null may come from deserializator
this.generatedSourcesFolder = generatedSourcesFolder;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -147,6 +174,7 @@ public class MavenImportingSettings implements Cloneable {
if (lookForNested != that.lookForNested) return false;
if (keepSourceFolders != that.keepSourceFolders) return false;
if (useMavenOutput != that.useMavenOutput) return false;
if (generatedSourcesFolder != that.generatedSourcesFolder) return false;
if (!dedicatedModuleDir.equals(that.dedicatedModuleDir)) return false;
if (updateFoldersOnImportPhase != null
? !updateFoldersOnImportPhase.equals(that.updateFoldersOnImportPhase)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.jetbrains.idea.maven.project.MavenImportingSettingsForm">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="15" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="myPanel" layout-manager="GridLayoutManager" row-count="16" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="522" height="501"/>
@@ -42,7 +42,7 @@
</component>
<component id="27a65" class="javax.swing.JLabel">
<constraints>
<grid row="11" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="1" use-parent-layout="false"/>
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="1" use-parent-layout="false"/>
</constraints>
<properties>
<text value="&lt;html&gt;IDEA needs to execute one of the listed phases in order to discover all&lt;br&gt;source folders that are configured via Maven plugins.&lt;br&gt;&lt;b&gt;Note&lt;/b&gt; that all test-* phases firstly generate and compile production sources.&lt;/html&gt;"/>
@@ -59,7 +59,7 @@
<grid id="49dc8" layout-manager="GridLayoutManager" row-count="1" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="10" left="5" bottom="0" right="0"/>
<constraints>
<grid row="12" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="13" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<focusable value="true"/>
@@ -108,7 +108,7 @@
<grid id="d7645" binding="myAdditionalSettingsPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="15" left="0" bottom="0" right="0"/>
<constraints>
<grid row="13" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
<grid row="14" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="8" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -116,7 +116,7 @@
</grid>
<hspacer id="d45a7">
<constraints>
<grid row="12" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="13" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<grid id="eca06" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
@@ -176,13 +176,13 @@
</grid>
<vspacer id="5d738">
<constraints>
<grid row="14" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="15" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<grid id="34bc4" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="5" bottom="0" right="0"/>
<constraints>
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="11" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@@ -211,7 +211,7 @@
</grid>
<vspacer id="aa40b">
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<grid row="10" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false">
<preferred-size width="-1" height="10"/>
</grid>
</constraints>
@@ -223,6 +223,35 @@
</grid>
</constraints>
</vspacer>
<grid id="f9c12" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="9" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="546c5" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Generated sources folders:"/>
</properties>
</component>
<hspacer id="3cc38">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="ec8ba" class="javax.swing.JComboBox" binding="myGeneratedSourcesComboBox">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
</grid>
</children>
</grid>
<buttonGroups>
@@ -15,12 +15,14 @@
*/
package org.jetbrains.idea.maven.project;
import com.intellij.ide.ui.ListCellRendererWrapper;
import com.intellij.ide.util.projectWizard.WizardContext;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.projectImport.ProjectFormatPanel;
import com.intellij.ui.EnumComboBoxModel;
import javax.swing.*;
import java.awt.event.ActionEvent;
@@ -48,6 +50,7 @@ public class MavenImportingSettingsForm {
private JPanel myAdditionalSettingsPanel;
private JPanel mySeparateModulesDirPanel;
private JComboBox myGeneratedSourcesComboBox;
public MavenImportingSettingsForm(boolean isImportStep, boolean isCreatingNewProject) {
mySearchRecursivelyCheckBox.setVisible(isImportStep);
@@ -66,6 +69,16 @@ public class MavenImportingSettingsForm {
FileChooserDescriptorFactory.createSingleFolderDescriptor());
myUpdateFoldersOnImportPhaseComboBox.setModel(new DefaultComboBoxModel(MavenImportingSettings.UPDATE_FOLDERS_PHASES));
myGeneratedSourcesComboBox.setModel(new EnumComboBoxModel<MavenImportingSettings.GeneratedSourcesFolder>(MavenImportingSettings.GeneratedSourcesFolder.class));
myGeneratedSourcesComboBox.setRenderer(new ListCellRendererWrapper(myGeneratedSourcesComboBox.getRenderer()) {
@Override
public void customize(JList list, Object value, int index, boolean selected, boolean hasFocus) {
if (value instanceof MavenImportingSettings.GeneratedSourcesFolder) {
setText(((MavenImportingSettings.GeneratedSourcesFolder)value).title);
}
}
});
}
private void createUIComponents() {
@@ -101,6 +114,7 @@ public class MavenImportingSettingsForm {
data.setUseMavenOutput(myUseMavenOutputCheckBox.isSelected());
data.setUpdateFoldersOnImportPhase((String)myUpdateFoldersOnImportPhaseComboBox.getSelectedItem());
data.setGeneratedSourcesFolder((MavenImportingSettings.GeneratedSourcesFolder)myGeneratedSourcesComboBox.getSelectedItem());
data.setDownloadSourcesAutomatically(myDownloadSourcesCheckBox.isSelected());
data.setDownloadDocsAutomatically(myDownloadDocsCheckBox.isSelected());
@@ -120,6 +134,7 @@ public class MavenImportingSettingsForm {
myUseMavenOutputCheckBox.setSelected(data.isUseMavenOutput());
myUpdateFoldersOnImportPhaseComboBox.setSelectedItem(data.getUpdateFoldersOnImportPhase());
myGeneratedSourcesComboBox.setSelectedItem(data.getGeneratedSourcesFolder());
myDownloadSourcesCheckBox.setSelected(data.isDownloadSourcesAutomatically());
myDownloadDocsCheckBox.setSelected(data.isDownloadDocsAutomatically());
@@ -21,6 +21,9 @@ import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.idea.maven.MavenCustomRepositoryHelper;
import org.jetbrains.idea.maven.MavenImportingTestCase;
import org.jetbrains.idea.maven.project.MavenGeneralSettings;
import org.jetbrains.idea.maven.project.MavenImportingSettings;
import org.jetbrains.idea.maven.project.MavenProjectsManager;
import org.jetbrains.idea.maven.utils.Path;
import java.io.File;
@@ -530,10 +533,11 @@ public class FoldersImportingTest extends MavenImportingTestCase {
public void testAddingExistingGeneratedSources() throws Exception {
createStdProjectFolders();
createProjectSubDirs("target/generated-sources/src1",
"target/generated-sources/src2",
"target/generated-test-sources/test1",
"target/generated-test-sources/test2");
createProjectSubFile("target/generated-sources/src1/com/A.java", "package com; class A {}");
createProjectSubFile("target/generated-sources/src2/com/B.java", "package com; class B {}");
createProjectSubFile("target/generated-test-sources/test1/com/test/A.java", "package com.test; class A {}");
createProjectSubFile("target/generated-test-sources/test2/com/test/B.java", "package com.test; class B {}");
importProject("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
@@ -552,6 +556,39 @@ public class FoldersImportingTest extends MavenImportingTestCase {
"target/generated-test-sources/test2");
}
public void testAddingExistingGeneratedSources2() throws Exception {
createStdProjectFolders();
createProjectSubFile("target/generated-sources/com/A.java", "package com; class A {}");
importProject("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
"<version>1</version>");
assertSources("project",
"src/main/java",
"src/main/resources",
"target/generated-sources");
}
public void testAddingExistingGeneratedSources3() throws Exception {
createStdProjectFolders();
MavenProjectsManager.getInstance(myProject).getImportingSettings().setGeneratedSourcesFolder(
MavenImportingSettings.GeneratedSourcesFolder.SUBFOLDER);
createProjectSubFile("target/generated-sources/com/A.java", "package com; class A {}");
importProject("<groupId>test</groupId>" +
"<artifactId>project</artifactId>" +
"<version>1</version>");
assertSources("project",
"src/main/java",
"src/main/resources",
"target/generated-sources/com");
}
public void testAddingExistingGeneratedSourcesWithCustomTargetDir() throws Exception {
createStdProjectFolders();
createProjectSubDirs("targetCustom/generated-sources/src",