IDEA-133754 Maven: resources builder: support outputDirectory parameters of maven...
authorVladislav.Soroka <Vladislav.Soroka@jetbrains.com>
Tue, 2 Dec 2014 13:27:25 +0000 (16:27 +0300)
committerVladislav.Soroka <Vladislav.Soroka@jetbrains.com>
Mon, 8 Dec 2014 09:00:10 +0000 (12:00 +0300)
plugins/maven/jps-plugin/src/org/jetbrains/jps/maven/compiler/MavenResourcesBuilder.java
plugins/maven/jps-plugin/src/org/jetbrains/jps/maven/model/impl/MavenModuleResourceConfiguration.java
plugins/maven/src/main/java/org/jetbrains/idea/maven/project/MavenResourceCompilerConfigurationGenerator.java
plugins/maven/src/test/java/org/jetbrains/idea/maven/compiler/ResourceCopyingTest.java

index 1df416dc0fe1f11bae440cbf1cb7b0c95ffc40ce..69184dd04fd6cf73685eed7cb5d0fea8002415df 100644 (file)
@@ -98,7 +98,8 @@ public class MavenResourcesBuilder extends TargetBuilder<MavenResourceRootDescri
         if (relPath == null) {
           continue;
         }
-        final File outputDir = MavenResourcesTarget.getOutputDir(target.getModuleOutputDir(), rd.getConfiguration(), config.outputDirectory);
+        final String outputDirectory = target.isTests() ? config.testOutputDirectory : config.outputDirectory;
+        final File outputDir = MavenResourcesTarget.getOutputDir(target.getModuleOutputDir(), rd.getConfiguration(), outputDirectory);
         if (outputDir == null) {
           continue;
         }
index c7f27c40075ea98a0ddeba806cddc59737766aa9..395e22929b5dbdf4ab63d3e8cc177667d5ed841b 100644 (file)
@@ -69,6 +69,9 @@ public class MavenModuleResourceConfiguration {
   @OptionTag
   public String outputDirectory = null;
 
+  @OptionTag
+  public String testOutputDirectory = null;
+
   @Tag("resources")
   @AbstractCollection(surroundWithTag = false, elementTag = "resource")
   public List<ResourceRootConfiguration> resources = new ArrayList<ResourceRootConfiguration>();
@@ -108,6 +111,7 @@ public class MavenModuleResourceConfiguration {
     result = 31 * result + filteringExclusions.hashCode();
     result = 31 * result + (escapeString != null ? escapeString.hashCode() : 0);
     result = 31 * result + (outputDirectory != null ? outputDirectory.hashCode() : 0);
+    result = 31 * result + (testOutputDirectory != null ? testOutputDirectory.hashCode() : 0);
     result = 31 * result + (escapeWindowsPaths ? 1 : 0);
     result = 31 * result + (overwrite ? 1 : 0);
     return result;
index 86f73e9721fa391bd8ea1ddafd01578a20a79283..a245114ad5e6cc10335b4258c8c990ef34c4bc27 100644 (file)
@@ -23,6 +23,7 @@ import com.intellij.util.xmlb.XmlSerializer;
 import org.jdom.Document;
 import org.jdom.Element;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.jetbrains.idea.maven.dom.references.MavenFilteredPropertyPsiReferenceProvider;
 import org.jetbrains.idea.maven.model.MavenId;
 import org.jetbrains.idea.maven.model.MavenResource;
@@ -124,7 +125,9 @@ public class MavenResourceCompilerConfigurationGenerator {
       }
 
       Element pluginConfiguration = mavenProject.getPluginConfiguration("org.apache.maven.plugins", "maven-resources-plugin");
-      resourceConfig.outputDirectory = MavenJDOMUtil.findChildValueByPath(pluginConfiguration, "outputDirectory", null);
+
+      resourceConfig.outputDirectory = getResourcesPluginGoalOutputDirectory(mavenProject, pluginConfiguration, "resources");
+      resourceConfig.testOutputDirectory = getResourcesPluginGoalOutputDirectory(mavenProject, pluginConfiguration, "testResources");
 
       addResources(resourceConfig.resources, mavenProject.getResources());
       addResources(resourceConfig.testResources, mavenProject.getTestResources());
@@ -182,6 +185,20 @@ public class MavenResourceCompilerConfigurationGenerator {
     });
   }
 
+  @Nullable
+  private static String getResourcesPluginGoalOutputDirectory(@NotNull MavenProject mavenProject,
+                                                              @Nullable Element pluginConfiguration,
+                                                              @NotNull String goal) {
+    final Element goalConfiguration = mavenProject.getPluginGoalConfiguration("org.apache.maven.plugins", "maven-resources-plugin", goal);
+    String outputDirectory = MavenJDOMUtil.findChildValueByPath(goalConfiguration, "outputDirectory", null);
+    if (outputDirectory == null) {
+      outputDirectory = MavenJDOMUtil.findChildValueByPath(pluginConfiguration, "outputDirectory", null);
+    }
+    return outputDirectory == null || FileUtil.isAbsolute(outputDirectory)
+           ? outputDirectory
+           : mavenProject.getDirectory() + '/' + outputDirectory;
+  }
+
   private static void generateManifest(@NotNull MavenProject mavenProject, @NotNull Module module) {
     try {
       String jdkVersion = null;
index 342c1b582d0ee1eb4d940dbb8511ddd2e98d9a91..1684caafd020f36637d22665b63ce4ba298a39aa 100644 (file)
@@ -122,6 +122,70 @@ public class ResourceCopyingTest extends MavenCompilingTestCase {
     assertCopied("target/resourceOutput/foo/dir/file.properties");
   }
 
+  public void testResourcesPluginGoalAbsoluteCustomTargetPath() throws Exception {
+    createProjectSubFile("src/test/resources/dir/file.properties");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <artifactId>maven-resources-plugin</artifactId>" +
+                  "      <version>2.6</version>" +
+                  "      <executions>" +
+                  "       <execution>" +
+                  "         <id>default-testResources</id>" +
+                  "         <phase>process-test-resources</phase>" +
+                  "         <goals>" +
+                  "           <goal>testResources</goal>" +
+                  "         </goals>" +
+                  "         <configuration>" +
+                  "           <outputDirectory>${project.build.testOutputDirectory}/custom</outputDirectory>" +
+                  "         </configuration>" +
+                  "       </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    compileModules("project");
+    assertCopied("target/test-classes/custom/dir/file.properties");
+  }
+
+  public void testResourcesPluginGoalRelativeCustomTargetPath() throws Exception {
+    createProjectSubFile("src/test/resources/dir/file.properties");
+
+    importProject("<groupId>test</groupId>" +
+                  "<artifactId>project</artifactId>" +
+                  "<version>1</version>" +
+
+                  "<build>" +
+                  "  <plugins>" +
+                  "    <plugin>" +
+                  "      <artifactId>maven-resources-plugin</artifactId>" +
+                  "      <version>2.6</version>" +
+                  "      <executions>" +
+                  "       <execution>" +
+                  "         <id>default-testResources</id>" +
+                  "         <phase>process-test-resources</phase>" +
+                  "         <goals>" +
+                  "           <goal>testResources</goal>" +
+                  "         </goals>" +
+                  "         <configuration>" +
+                  "           <outputDirectory>target/test-classes/custom</outputDirectory>" +
+                  "         </configuration>" +
+                  "       </execution>" +
+                  "      </executions>" +
+                  "    </plugin>" +
+                  "  </plugins>" +
+                  "</build>");
+
+    compileModules("project");
+    assertCopied("target/test-classes/custom/dir/file.properties");
+  }
+
   public void testAbsoluteCustomTargetPath() throws Exception {
     createProjectSubFile("res/foo/file.properties");