You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2020/02/21 15:06:22 UTC

[sling-ide-tooling] branch master updated: SLING-9117 improve guessing of jcr_root

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-ide-tooling.git


The following commit(s) were added to refs/heads/master by this push:
     new 9e426c6  SLING-9117 improve guessing of jcr_root
9e426c6 is described below

commit 9e426c6cee2131f4cfa2cc0008cd3d4cbf651e70
Author: Konrad Windszus <kw...@apache.org>
AuthorDate: Fri Feb 21 15:01:09 2020 +0100

    SLING-9117 improve guessing of jcr_root
---
 .../LegacyMavenBundleProjectTest.java              |  2 +-
 .../m2e/{ => internal}/MavenProjectUtilsTest.java  | 23 ++++++++++++++-
 .../ide/eclipse/m2e/{ => internal}/legacy-pom.xml  |  0
 .../project1/src/main/content/jcr_root/marker.txt  | 16 ++++++++++
 .../main/content/intermediate/jcr_root/marker.txt  | 16 ++++++++++
 .../m2e/{ => internal}/slingstart-simple-pom.xml   |  0
 .../ContentPackageProjectConfigurator.java         | 34 +++++++++++++---------
 .../eclipse/m2e/internal/MavenProjectUtils.java    | 28 +++++++++++-------
 8 files changed, 93 insertions(+), 26 deletions(-)

diff --git a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/LegacyMavenBundleProjectTest.java b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/LegacyMavenBundleProjectTest.java
similarity index 98%
rename from eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/LegacyMavenBundleProjectTest.java
rename to eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/LegacyMavenBundleProjectTest.java
index b582aba..f313802 100644
--- a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/LegacyMavenBundleProjectTest.java
+++ b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/LegacyMavenBundleProjectTest.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.ide.eclipse.m2e;
+package org.apache.sling.ide.eclipse.m2e.internal;
 
 import java.util.concurrent.TimeUnit;
 
diff --git a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/MavenProjectUtilsTest.java b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtilsTest.java
similarity index 68%
rename from eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/MavenProjectUtilsTest.java
rename to eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtilsTest.java
index 4b729a6..bdea1ab 100644
--- a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/MavenProjectUtilsTest.java
+++ b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtilsTest.java
@@ -14,10 +14,14 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.ide.eclipse.m2e;
+package org.apache.sling.ide.eclipse.m2e.internal;
 
 import static org.hamcrest.CoreMatchers.equalTo;
 
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.Optional;
 import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 
@@ -28,6 +32,7 @@ import org.apache.sling.ide.test.impl.helpers.TemporaryProject;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.Path;
+import org.junit.Assert;
 import org.junit.Rule;
 import org.junit.Test;
 
@@ -58,4 +63,20 @@ public class MavenProjectUtilsTest {
 		}, equalTo(modelsDir));
         
 	}
+	
+	@Test
+	public void testGuessJcrRootFolder() throws IOException {
+	    java.nio.file.Path rootPath = Paths.get("src", "org", "apache", "sling", "ide", "eclipse", "m2e", "internal", "project1");
+	    Assert.assertTrue("rootPath not found", Files.exists(rootPath));
+	    // create folder structure, 
+	    Optional<java.nio.file.Path> actualJcrRoot = MavenProjectUtils.guessJcrRootFolder(rootPath);
+	    Assert.assertTrue(actualJcrRoot.isPresent());
+	    Assert.assertEquals(Paths.get("src", "main", "content", "jcr_root"), actualJcrRoot.get());
+	    
+	    // test jcr_root beyond level 4
+	    rootPath = Paths.get("src", "org", "apache", "sling", "ide", "eclipse", "m2e", "internal", "project2");
+        Assert.assertTrue("rootPath not found", Files.exists(rootPath));
+        actualJcrRoot = MavenProjectUtils.guessJcrRootFolder(rootPath);
+        Assert.assertFalse(actualJcrRoot.isPresent());
+	}
 }
diff --git a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/legacy-pom.xml b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/legacy-pom.xml
similarity index 100%
rename from eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/legacy-pom.xml
rename to eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/legacy-pom.xml
diff --git a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/project1/src/main/content/jcr_root/marker.txt b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/project1/src/main/content/jcr_root/marker.txt
new file mode 100644
index 0000000..67f297c
--- /dev/null
+++ b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/project1/src/main/content/jcr_root/marker.txt
@@ -0,0 +1,16 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or
+    more contributor license agreements. See the NOTICE file
+    distributed with this work for additional information regarding
+    copyright ownership. The ASF licenses this file to you under the
+    Apache License, Version 2.0 (the "License"); you may not use
+    this file except in compliance with the License. You may obtain
+    a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+    applicable law or agreed to in writing, software distributed
+    under the License is distributed on an "AS IS" BASIS, WITHOUT
+    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions
+    and limitations under the License.
+-->
\ No newline at end of file
diff --git a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/project2/src/main/content/intermediate/jcr_root/marker.txt b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/project2/src/main/content/intermediate/jcr_root/marker.txt
new file mode 100644
index 0000000..67f297c
--- /dev/null
+++ b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/project2/src/main/content/intermediate/jcr_root/marker.txt
@@ -0,0 +1,16 @@
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or
+    more contributor license agreements. See the NOTICE file
+    distributed with this work for additional information regarding
+    copyright ownership. The ASF licenses this file to you under the
+    Apache License, Version 2.0 (the "License"); you may not use
+    this file except in compliance with the License. You may obtain
+    a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0 Unless required by
+    applicable law or agreed to in writing, software distributed
+    under the License is distributed on an "AS IS" BASIS, WITHOUT
+    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions
+    and limitations under the License.
+-->
\ No newline at end of file
diff --git a/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/slingstart-simple-pom.xml b/eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/slingstart-simple-pom.xml
similarity index 100%
rename from eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/slingstart-simple-pom.xml
rename to eclipse/eclipse-m2e-test/src/org/apache/sling/ide/eclipse/m2e/internal/slingstart-simple-pom.xml
diff --git a/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/ContentPackageProjectConfigurator.java b/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/ContentPackageProjectConfigurator.java
index b30a813..6b99529 100644
--- a/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/ContentPackageProjectConfigurator.java
+++ b/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/ContentPackageProjectConfigurator.java
@@ -16,13 +16,13 @@
  */
 package org.apache.sling.ide.eclipse.m2e.internal;
 
-import java.nio.file.Paths;
+import java.io.IOException;
 import java.util.Arrays;
 import java.util.LinkedHashSet;
+import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
 
-import org.apache.maven.model.Resource;
 import org.apache.maven.project.MavenProject;
 import org.apache.sling.ide.eclipse.core.ConfigurationHelper;
 import org.eclipse.aether.util.StringUtils;
@@ -88,18 +88,24 @@ public class ContentPackageProjectConfigurator extends AbstractProjectConfigurat
         
         // core configuration for sling ide plugin
         
-        Resource folder = MavenProjectUtils.guessJcrRootFolder(mavenProject);
-        
-        java.nio.file.Path contentSyncPath = mavenProject.getBasedir().toPath().relativize(Paths.get(folder.getDirectory()));
-        
-        String jcrRootPath = contentSyncPath.toString();
-        ConfigurationHelper.convertToContentPackageProject(project, progressMonitor, Path.fromOSString(jcrRootPath));   
-        
-        if (getPreferences().isWtpFacetsEnabledInContentPackageProjectConfigurator()) {
-            new WtpProjectConfigurer(configRequest, project, jcrRootPath).configure(progressMonitor);
-            trace("WTP facets for {0} added", mavenProject);
-        } else {
-            trace("WTP facets for packing type 'content-package' are disabled through preferences.");
+        try {
+            Optional<java.nio.file.Path> contentSyncPath = MavenProjectUtils.guessJcrRootFolder(mavenProject);
+            if (contentSyncPath.isPresent()) {
+                // add marker
+                addMarker(configRequest.getPom(), "Could not detect jcr_root path for this content package!", IMarker.SEVERITY_ERROR);
+            }
+            
+            String jcrRootPath = contentSyncPath.toString();
+            ConfigurationHelper.convertToContentPackageProject(project, progressMonitor, Path.fromOSString(jcrRootPath));   
+            
+            if (getPreferences().isWtpFacetsEnabledInContentPackageProjectConfigurator()) {
+                new WtpProjectConfigurer(configRequest, project, jcrRootPath).configure(progressMonitor);
+                trace("WTP facets for {0} added", mavenProject);
+            } else {
+                trace("WTP facets for packing type 'content-package' are disabled through preferences.");
+            }
+        } catch (IOException e) {
+            Activator.getDefault().getPluginLogger().warn("Could not determine jcr_root for project: " + mavenProject.getBasedir() + ": "+e, e);
         }
         
         trace("Done converting .");
diff --git a/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtils.java b/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtils.java
index a459474..32aada9 100644
--- a/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtils.java
+++ b/eclipse/eclipse-m2e-ui/src/org/apache/sling/ide/eclipse/m2e/internal/MavenProjectUtils.java
@@ -16,16 +16,20 @@
  */
 package org.apache.sling.ide.eclipse.m2e.internal;
 
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.LinkedHashSet;
 import java.util.List;
+import java.util.Optional;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
+import java.util.stream.Stream;
 
 import org.apache.maven.model.Dependency;
 import org.apache.maven.model.Plugin;
-import org.apache.maven.model.Resource;
 import org.apache.maven.project.MavenProject;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 
@@ -33,18 +37,22 @@ public class MavenProjectUtils {
 
     private static final String DEFAULT_SERVLET_API_VERSION = "2.5";
     private static final Pattern SERVLET_API_VERSION_MATCHER = Pattern.compile("^(\\d\\.\\d)");
+    private static final int MAX_RELATIVE_DEPTH_OF_JCR_ROOT = 4; // relative depth of jcr_root in content packages from the Maven basedir
 
-    public static Resource guessJcrRootFolder(MavenProject project) {
-        
-        for ( Resource resource : project.getBuild().getResources() ) {
-            if ( resource.getDirectory().endsWith("jcr_root")) {
-                return resource;
+    public static Optional<Path> guessJcrRootFolder(MavenProject project) throws IOException {
+        return guessJcrRootFolder(project.getBasedir().toPath());
+    }
+
+    static Optional<Path> guessJcrRootFolder(Path baseDir) throws IOException {
+        try (Stream<Path> stream = Files.find(baseDir, MAX_RELATIVE_DEPTH_OF_JCR_ROOT, (a, b) -> a.endsWith("jcr_root"))) {
+            Optional<Path> jcrRoot = stream.findFirst();
+            if (jcrRoot.isPresent()) {
+                jcrRoot = Optional.of(baseDir.relativize(jcrRoot.get()));
             }
+            return jcrRoot;
         }
-        
-        return project.getBuild().getResources().get(0);
-    }
-    
+    } 
+
     public static String guessServletApiVersion(MavenProject project) {
         
         for ( Dependency dependency :  project.getDependencies() ) {