You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by si...@apache.org on 2019/04/24 13:51:57 UTC

[sling-org-apache-sling-feature-cpconverter] 05/15: allow the tool generating Features and deplopying Artifacts in two different directories

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

simonetripodi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-feature-cpconverter.git

commit 4acbf58e08c3592f6a92e2fc1b459167c2f89b1b
Author: stripodi <st...@192.168.1.111>
AuthorDate: Thu Apr 11 17:47:39 2019 +0200

    allow the tool generating Features and deplopying Artifacts in two
    different directories
---
 README.md                                          | 26 ++++++++++------
 .../ContentPackage2FeatureModelConverter.java      | 35 +++++++++++++--------
 .../cpconverter/DefaultBundlesDeployer.java        |  4 +--
 ...ntentPackage2FeatureModelConverterLauncher.java | 10 ++++--
 .../ContentPackage2FeatureModelConverterTest.java  | 36 ++++++++++++----------
 .../cpconverter/DefaultBundlesDeployerTest.java    |  1 -
 .../handlers/BundleEntryHandlerTest.java           |  6 ++--
 7 files changed, 71 insertions(+), 47 deletions(-)

diff --git a/README.md b/README.md
index 3072730..2dd6c3f 100644
--- a/README.md
+++ b/README.md
@@ -205,7 +205,7 @@ bundles are collected in an _Apache Maven repository_ compliant directory, all o
 
 ```
 $ tree bundles/
-bundles/
+artifacts/
 └── org
     └── apache
         ├── felix
@@ -265,8 +265,10 @@ new ContentPackage2FeatureModelConverter()
             .setMergeConfigurations(mergeConfigurations)
             // users can decide which is the bundles start order, declared in the generated Apache Sling Feature(s)
             .setBundlesStartOrder(bundlesStartOrder)
-            // a valid directory where the outputs will be generated (it will created, if not existing already)
-            .setOutputDirectory(outputDirectory)
+            // a valid directory where the artifacts will be deployed (it will created, if not existing already)
+            .setArtifactsOutputDirectory(outputDirectory)
+            // a valid directory where the Feature Models will be generated (it will created, if not existing already)
+            .setFeatureModelsOutputDirectory(sameOrDifferentOutputDirectory)
             // an existing and valid content-package file
             .convert(contentPackage);
 ```
@@ -348,9 +350,14 @@ once the package is decompressed, open the shell and type:
 
 ```
 $ ./bin/cp2sf -h
-Usage: cp2fm [-hmqsvX] [-b=<bundlesStartOrder>] -c=<contentPackage>
-             -o=<outputDirectory> [-f=<filteringPatterns>]...
+Missing required options [--content-package=<contentPackage>, --artifacts-output-directory=<artifactsOutputDirectory>, --features-output-directory=<featureModelsOutputDirectory>]
+Usage: cp2fm [-hmqsvX] -a=<artifactsOutputDirectory> [-b=<bundlesStartOrder>]
+             -c=<contentPackage> -o=<featureModelsOutputDirectory>
+             [-f=<filteringPatterns>]...
 Apache Sling Content Package to Sling Feature converter
+  -a, --artifacts-output-directory=<artifactsOutputDirectory>
+                            The output directory where the artifacts will be
+                              deployed.
   -b, --bundles-start-order=<bundlesStartOrder>
                             The order to start detected bundles.
   -c, --content-package=<contentPackage>
@@ -362,21 +369,22 @@ Apache Sling Content Package to Sling Feature converter
   -m, --merge-configurations
                             Flag to mark OSGi configurations with same PID will be
                               merged, the tool will fail otherwise.
-  -o, --output-directory=<outputDirectory>
-                            The output directory where the Feature File and the
-                              bundles will be deployed.
+  -o, --features-output-directory=<featureModelsOutputDirectory>
+                            The output directory where the Feature File will be
+                              generated.
   -q, --quiet               Log errors only.
   -s, --strict-validation   Flag to mark the content-package input file being strict
                               validated.
   -v, --version             Display version information.
   -X, --verbose             Produce execution debug output.
 Copyright(c) 2019 The Apache Software Foundation.
+
 ```
 
 to see all the available options; a sample execution could look like:
 
 ```
-$ ./bin/cp2sf -v -b 20 -c /content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/test-content-package.zip -o /tmp
+$ ./bin/cp2sf -v -b 20 -c /content-package-2-feature-model/src/test/resources/org/apache/sling/cp2fm/test-content-package.zip -a /cache -o /tmp
 ```
 
 Argument Files for Long Command Lines:
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
index 71ffa8e..e0e1c32 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverter.java
@@ -84,7 +84,9 @@ public class ContentPackage2FeatureModelConverter {
 
     private int bundlesStartOrder = 0;
 
-    private File outputDirectory;
+    private File artifactsOutputDirectory;
+
+    private File featureModelsOutputDirectory;
 
     private Feature targetFeature = null;
 
@@ -113,13 +115,18 @@ public class ContentPackage2FeatureModelConverter {
         return this;
     }
 
-    public ContentPackage2FeatureModelConverter setOutputDirectory(File outputDirectory) {
-        this.outputDirectory = outputDirectory;
+    public ContentPackage2FeatureModelConverter setArtifactsOutputDirectory(File artifactsOutputDirectory) {
+        this.artifactsOutputDirectory = artifactsOutputDirectory;
         return this;
     }
 
-    public File getOutputDirectory() {
-        return outputDirectory;
+    public File getArtifactsOutputDirectory() {
+        return artifactsOutputDirectory;
+    }
+
+    public ContentPackage2FeatureModelConverter setFeatureModelsOutputDirectory(File featureModelsOutputDirectory) {
+        this.featureModelsOutputDirectory = featureModelsOutputDirectory;
+        return this;
     }
 
     public Feature getTargetFeature() {
@@ -166,20 +173,24 @@ public class ContentPackage2FeatureModelConverter {
                                             + " does not exist or it is not a valid file.");
         }
 
-        if (outputDirectory == null) {
-            throw new IllegalStateException("Null output directory not supported, it must be set before invoking the convert(File) method.");
+        if (artifactsOutputDirectory == null) {
+            throw new IllegalStateException("Null artifacts output directory not supported, it must be set before invoking the convert(File) method.");
+        }
+
+        if (featureModelsOutputDirectory == null) {
+            throw new IllegalStateException("Null models output directory not supported, it must be set before invoking the convert(File) method.");
         }
 
         Iterator<BundlesDeployer> artifactDeployerLoader = ServiceLoader.load(BundlesDeployer.class).iterator();
         if (!artifactDeployerLoader.hasNext()) {
-            artifactDeployer = new DefaultBundlesDeployer(outputDirectory);
+            artifactDeployer = new DefaultBundlesDeployer(artifactsOutputDirectory);
         } else {
             artifactDeployer = artifactDeployerLoader.next();
         }
 
-        if (!outputDirectory.exists() && !outputDirectory.mkdirs()) {
+        if (!artifactsOutputDirectory.exists() && !artifactsOutputDirectory.mkdirs()) {
             throw new IllegalStateException("output directory "
-                                            + outputDirectory
+                                            + artifactsOutputDirectory
                                             + " does not exist and can not be created, please make sure current user '"
                                             + System.getProperty("user.name")
                                             + " has enough rights to write on the File System.");
@@ -222,7 +233,7 @@ public class ContentPackage2FeatureModelConverter {
 
             // attach all unmatched resources as new content-package
 
-            File contentPackageArchive = mainPackageAssembler.createPackage(outputDirectory);
+            File contentPackageArchive = mainPackageAssembler.createPackage(artifactsOutputDirectory);
 
             // deploy the new zip content-package to the local mvn bundles dir
 
@@ -308,7 +319,7 @@ public class ContentPackage2FeatureModelConverter {
 
         fileName.append(JSON_FILE_EXTENSION);
 
-        File targetFile = new File(outputDirectory, fileName.toString());
+        File targetFile = new File(featureModelsOutputDirectory, fileName.toString());
 
         logger.info("Conversion complete!", targetFile);
         logger.info("Writing resulting Feature File to '{}'...", targetFile);
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployer.java b/src/main/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployer.java
index 780aed8..7921074 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployer.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployer.java
@@ -29,14 +29,12 @@ import org.slf4j.LoggerFactory;
 
 public final class DefaultBundlesDeployer implements BundlesDeployer {
 
-    private static final String BUNDLES_RIRECTORY_NAME = "bundles";
-
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
     private final File artifactsDirectory;
 
     public DefaultBundlesDeployer(File outputDirectory) {
-        artifactsDirectory = new File(outputDirectory, BUNDLES_RIRECTORY_NAME);
+        artifactsDirectory = outputDirectory;
         if (!artifactsDirectory.exists()) {
             artifactsDirectory.mkdirs();
         }
diff --git a/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java b/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
index 7aa7c50..cba9553 100644
--- a/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
+++ b/src/main/java/org/apache/sling/feature/cpconverter/cli/ContentPackage2FeatureModelConverterLauncher.java
@@ -61,8 +61,11 @@ public final class ContentPackage2FeatureModelConverterLauncher implements Runna
     @Option(names = { "-f", "--filtering-patterns" }, description = "Regex based pattern(s) to reject content-package archive entries.", required = false)
     private String[] filteringPatterns;
 
-    @Option(names = { "-o", "--output-directory" }, description = "The output directory where the Feature File and the bundles will be deployed.", required = true)
-    private File outputDirectory;
+    @Option(names = { "-a", "--artifacts-output-directory" }, description = "The output directory where the artifacts will be deployed.", required = true)
+    private File artifactsOutputDirectory;
+
+    @Option(names = { "-o", "--features-output-directory" }, description = "The output directory where the Feature File will be generated.", required = true)
+    private File featureModelsOutputDirectory;
 
     @Override
     public void run() {
@@ -96,7 +99,8 @@ public final class ContentPackage2FeatureModelConverterLauncher implements Runna
                                                              .setStrictValidation(strictValidation)
                                                              .setMergeConfigurations(mergeConfigurations)
                                                              .setBundlesStartOrder(bundlesStartOrder)
-                                                             .setOutputDirectory(outputDirectory);
+                                                             .setArtifactsOutputDirectory(artifactsOutputDirectory)
+                                                             .setFeatureModelsOutputDirectory(featureModelsOutputDirectory);
 
             if (filteringPatterns != null && filteringPatterns.length > 0) {
                 for (String filteringPattern : filteringPatterns) {
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java b/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
index 3c16634..6db646b 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/ContentPackage2FeatureModelConverterTest.java
@@ -121,7 +121,10 @@ public class ContentPackage2FeatureModelConverterTest {
 
         File outputDirectory = new File(System.getProperty("testDirectory"), getClass().getName() + '_' + System.currentTimeMillis());
 
-        converter.setBundlesStartOrder(5).setOutputDirectory(outputDirectory).convert(packageFile);
+        converter.setBundlesStartOrder(5)
+                 .setArtifactsOutputDirectory(outputDirectory)
+                 .setFeatureModelsOutputDirectory(outputDirectory)
+                 .convert(packageFile);
 
         verifyFeatureFile(outputDirectory,
                           "asd.retail.all.json",
@@ -142,7 +145,7 @@ public class ContentPackage2FeatureModelConverterTest {
                           Arrays.asList("org.apache.sling.serviceusermapping.impl.ServiceUserMapperImpl.amended-asd-retail"),
                           Collections.emptyList());
 
-        ZipFile zipFile = new ZipFile(new File(outputDirectory, "bundles/asd/sample/asd.retail.all/0.0.1/asd.retail.all-0.0.1-cp2fm-converted-feature.zip"));
+        ZipFile zipFile = new ZipFile(new File(outputDirectory, "asd/sample/asd.retail.all/0.0.1/asd.retail.all-0.0.1-cp2fm-converted-feature.zip"));
         for (String expectedEntry : new String[] {
                 "jcr_root/content/asd/.content.xml",
                 "jcr_root/content/asd/resources.xml",
@@ -164,7 +167,7 @@ public class ContentPackage2FeatureModelConverterTest {
     private void verifyFeatureFile(File outputDirectory,
                                    String name,
                                    String expectedArtifactId,
-                                   List<String> expectedBundles,
+                                   List<String> expectedArtifacts,
                                    List<String> expectedConfigurations,
                                    List<String> expectedContentPackagesExtensions) throws Exception {
         File featureFile = new File(outputDirectory, name);
@@ -175,9 +178,9 @@ public class ContentPackage2FeatureModelConverterTest {
 
             assertEquals(expectedArtifactId, feature.getId().toMvnId());
 
-            for (String expectedBundle : expectedBundles) {
-                assertTrue(expectedBundle + " not found in Feature " + expectedArtifactId, feature.getBundles().containsExact(ArtifactId.fromMvnId(expectedBundle)));
-                verifyInstalledBundle(outputDirectory, expectedBundle);
+            for (String expectedArtifact : expectedArtifacts) {
+                assertTrue(expectedArtifact + " not found in Feature " + expectedArtifactId, feature.getBundles().containsExact(ArtifactId.fromMvnId(expectedArtifact)));
+                verifyInstalledArtifact(outputDirectory, expectedArtifact);
             }
 
             for (String expectedConfiguration : expectedConfigurations) {
@@ -187,23 +190,21 @@ public class ContentPackage2FeatureModelConverterTest {
             for (String expectedContentPackagesExtension : expectedContentPackagesExtensions) {
                 assertTrue(expectedContentPackagesExtension + " not found in Feature " + expectedArtifactId,
                            feature.getExtensions().getByName("content-packages").getArtifacts().containsExact(ArtifactId.fromMvnId(expectedContentPackagesExtension)));
-                verifyInstalledBundle(outputDirectory, expectedContentPackagesExtension);
+                verifyInstalledArtifact(outputDirectory, expectedContentPackagesExtension);
             }
         }
     }
 
-    private void verifyInstalledBundle(File outputDirectory, String coordinates) {
+    private void verifyInstalledArtifact(File outputDirectory, String coordinates) {
         ArtifactId bundleId = ArtifactId.fromMvnId(coordinates);
 
-        File bundleDirectory = new File(outputDirectory, "bundles");
-
         StringTokenizer tokenizer = new StringTokenizer(bundleId.getGroupId(), ".");
         while (tokenizer.hasMoreTokens()) {
-            bundleDirectory = new File(bundleDirectory, tokenizer.nextToken());
+            outputDirectory = new File(outputDirectory, tokenizer.nextToken());
         }
 
-        bundleDirectory = new File(bundleDirectory, bundleId.getArtifactId());
-        bundleDirectory = new File(bundleDirectory, bundleId.getVersion());
+        outputDirectory = new File(outputDirectory, bundleId.getArtifactId());
+        outputDirectory = new File(outputDirectory, bundleId.getVersion());
 
         StringBuilder bundleFileName = new StringBuilder()
                                        .append(bundleId.getArtifactId())
@@ -214,10 +215,10 @@ public class ContentPackage2FeatureModelConverterTest {
         }
         bundleFileName.append('.').append(bundleId.getType());
 
-        File bundleFile = new File(bundleDirectory, bundleFileName.toString());
+        File bundleFile = new File(outputDirectory, bundleFileName.toString());
         assertTrue("Bundle " + bundleFile + " does not exist", bundleFile.exists());
 
-        File pomFile = new File(bundleDirectory, String.format("%s-%s.pom", bundleId.getArtifactId(), bundleId.getVersion()));
+        File pomFile = new File(outputDirectory, String.format("%s-%s.pom", bundleId.getArtifactId(), bundleId.getVersion()));
         assertTrue("POM file " + pomFile + " does not exist", pomFile.exists());
     }
 
@@ -230,7 +231,10 @@ public class ContentPackage2FeatureModelConverterTest {
 
         File outputDirectory = new File(System.getProperty("testDirectory"), getClass().getName() + '_' + System.currentTimeMillis());
 
-        converter.setBundlesStartOrder(5).setOutputDirectory(outputDirectory).convert(packageFile);
+        converter.setBundlesStartOrder(5)
+                 .setArtifactsOutputDirectory(outputDirectory)
+                 .setFeatureModelsOutputDirectory(outputDirectory)
+                 .convert(packageFile);
     }
 
 }
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployerTest.java b/src/test/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployerTest.java
index 1fe0429..477f02b 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployerTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/DefaultBundlesDeployerTest.java
@@ -49,7 +49,6 @@ public class DefaultBundlesDeployerTest {
         assertNotNull(bundlesDirectory);
         assertTrue(bundlesDirectory.exists());
         assertTrue(bundlesDirectory.isDirectory());
-        assertEquals("bundles", bundlesDirectory.getName());
     }
 
     @Test(expected = NullPointerException.class)
diff --git a/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerTest.java b/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerTest.java
index 8753d61..b6db188 100644
--- a/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerTest.java
+++ b/src/test/java/org/apache/sling/feature/cpconverter/handlers/BundleEntryHandlerTest.java
@@ -84,7 +84,7 @@ public final class BundleEntryHandlerTest {
         ContentPackage2FeatureModelConverter converter = spy(ContentPackage2FeatureModelConverter.class);
 
         File testDirectory = new File(System.getProperty("testDirectory"), getClass().getName() + '_' + System.currentTimeMillis());
-        when(converter.getOutputDirectory()).thenReturn(testDirectory);
+        when(converter.getArtifactsOutputDirectory()).thenReturn(testDirectory);
 
         doCallRealMethod().when(converter).attach(anyString(), anyString(), anyString(), anyString(), anyString(), anyString());
         when(converter.getArtifactDeployer()).thenReturn(new DefaultBundlesDeployer(testDirectory));
@@ -95,8 +95,8 @@ public final class BundleEntryHandlerTest {
 
         bundleEntryHandler.handle(bundleLocation, archive, entry, converter);
 
-        assertTrue(new File(testDirectory, "bundles/org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.pom").exists());
-        assertTrue(new File(testDirectory, "bundles/org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.jar").exists());
+        assertTrue(new File(testDirectory, "org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.pom").exists());
+        assertTrue(new File(testDirectory, "org/apache/felix/org.apache.felix.framework/6.0.1/org.apache.felix.framework-6.0.1.jar").exists());
 
         assertFalse(converter.getTargetFeature().getBundles().isEmpty());
         assertEquals(1, feature.getBundles().size());