You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2020/09/17 07:58:08 UTC

[sling-slingfeature-maven-plugin] branch master updated: SLING-9737 : Provide an extension mechanism for api generation

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

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-slingfeature-maven-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new e252c02  SLING-9737 : Provide an extension mechanism for api generation
e252c02 is described below

commit e252c029a9dd5eee86b41679d9cddcad6e2100de
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Thu Sep 17 09:57:26 2020 +0200

    SLING-9737 : Provide an extension mechanism for api generation
---
 .../sling/feature/maven/mojos/ApisJarMojo.java     | 100 +++++++++++++--------
 .../feature/maven/mojos/apis/ApisJarContext.java   |  10 ++-
 .../sling/feature/maven/mojos/apis/ApisUtil.java   |  15 ++++
 .../feature/maven/mojos/apis/ArtifactType.java     |  54 +++++++++++
 .../feature/maven/mojos/apis/DirectorySource.java  |  69 ++++++++++++++
 .../sling/feature/maven/mojos/apis/FileSource.java |  48 ++++++++++
 .../feature/maven/mojos/apis/spi/Processor.java    |  47 ++++++++++
 .../maven/mojos/apis/spi/ProcessorContext.java     |  51 +++++++++++
 .../sling/feature/maven/mojos/apis/spi/Source.java |  38 ++++++++
 9 files changed, 391 insertions(+), 41 deletions(-)

diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
index 4383db3..f2cdcf6 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/ApisJarMojo.java
@@ -48,6 +48,7 @@ import org.apache.felix.utils.manifest.Clause;
 import org.apache.felix.utils.manifest.Parser;
 import org.apache.maven.archiver.MavenArchiveConfiguration;
 import org.apache.maven.archiver.MavenArchiver;
+import org.apache.maven.execution.MavenSession;
 import org.apache.maven.model.License;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.Scm;
@@ -60,6 +61,7 @@ import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
+import org.apache.maven.project.MavenProject;
 import org.apache.maven.repository.RepositorySystem;
 import org.apache.maven.scm.ScmException;
 import org.apache.maven.scm.ScmFileSet;
@@ -87,8 +89,14 @@ import org.apache.sling.feature.io.IOUtils;
 import org.apache.sling.feature.maven.mojos.apis.ApisJarContext;
 import org.apache.sling.feature.maven.mojos.apis.ApisJarContext.ArtifactInfo;
 import org.apache.sling.feature.maven.mojos.apis.ApisUtil;
+import org.apache.sling.feature.maven.mojos.apis.ArtifactType;
+import org.apache.sling.feature.maven.mojos.apis.DirectorySource;
+import org.apache.sling.feature.maven.mojos.apis.FileSource;
 import org.apache.sling.feature.maven.mojos.apis.JavadocExecutor;
 import org.apache.sling.feature.maven.mojos.apis.JavadocLinks;
+import org.apache.sling.feature.maven.mojos.apis.spi.Processor;
+import org.apache.sling.feature.maven.mojos.apis.spi.ProcessorContext;
+import org.apache.sling.feature.maven.mojos.apis.spi.Source;
 import org.codehaus.plexus.archiver.UnArchiver;
 import org.codehaus.plexus.archiver.jar.JarArchiver;
 import org.codehaus.plexus.archiver.manager.ArchiverManager;
@@ -108,44 +116,7 @@ import org.osgi.framework.Constants;
 )
 public class ApisJarMojo extends AbstractIncludingFeatureMojo {
 
-    public enum ArtifactType {
-        APIS("apis", "class", "jar"),
-        SOURCES("sources", "java", "jar"),
-        JAVADOC("javadoc", "html", "jar"),
-        DEPENDENCIES("apideps", "txt", "ref"),
-        CND("cnd", "cnd", "jar"),
-        REPORT("report", "txt", "txt");
-
-        private final String id;
-
-        private final String type;
-
-        private final String extension;
-
-        ArtifactType(final String id, final String type, final String extension) {
-            this.id = id;
-            this.type = type;
-            this.extension = extension;
-        }
-
-        public String getId() {
-            return this.id;
-        }
-
-        public String getContentType() {
-            return this.type;
-        }
-
-        public String getContentExtension() {
-            return ".".concat(this.type);
-        }
-
-        public String getExtension() {
-            return this.extension;
-        }
-    }
-
-    /**
+   /**
      * Select the features for api generation.
      * Separate api jars will be generated for each feature.
      */
@@ -1486,7 +1457,12 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
             final List<String> report) throws MojoExecutionException {
         final JarArchiver jarArchiver = new JarArchiver();
 
+        final List<Processor> processors;
+        final List<Source> sources;
+
         if ( archiveType == ArtifactType.APIS || archiveType == ArtifactType.SOURCES ) {
+            processors = ApisUtil.getProcessors();
+            sources = processors.isEmpty() ? null : new ArrayList<>();
             // api or source
             for(final ArtifactInfo info : infos) {
                 final File dir = archiveType == ArtifactType.APIS ? info.getBinDirectory() : info.getSourceDirectory();
@@ -1498,9 +1474,14 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                     fileSet.setIncludingEmptyDirectories(false);
                     fileSet.setIncludes(usedExportedPackageIncludes);
                     jarArchiver.addFileSet(fileSet);
+                    if ( sources != null ) {
+                        sources.add(new DirectorySource(fileSet));
+                    }
                 }
             }
         } else {
+            processors = Collections.emptyList();
+            sources = null;
             // javadoc
             final DefaultFileSet fileSet = new DefaultFileSet(ctx.getJavadocDir());
             jarArchiver.addFileSet(fileSet);
@@ -1513,6 +1494,9 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                 final String name = resource.getAbsolutePath().substring(prefixLength);
                 jarArchiver.addFile(resource, name);
                 getLog().debug("Adding resource " + name);
+                if ( sources != null ) {
+                    sources.add(new FileSource(info.getBinDirectory(), resource));
+                }
             }
         }
 
@@ -1529,9 +1513,49 @@ public class ApisJarMojo extends AbstractIncludingFeatureMojo {
                     for (String includedFile : ds.getIncludedFiles()) {
                         jarArchiver.addFile(new File(rsrc, includedFile), includedFile);
                     }
+                    if ( sources != null ) {
+                        final DefaultFileSet fileSet = new DefaultFileSet(rsrc);
+                        fileSet.setIncludingEmptyDirectories(false);
+                        fileSet.setIncludes(new String[] {"**/*.*"});
+                        sources.add(new DirectorySource(fileSet));
+                    }
                 } else {
                     jarArchiver.addFile(rsrc, rsrc.getName());
+                    if ( sources != null ) {
+                        sources.add(new FileSource(rsrc.getParentFile(), rsrc));
+                    }
+                }
+            }
+        }
+
+        // run processors
+        for(final Processor p : processors) {
+            final ProcessorContext pc = new ProcessorContext() {
+
+                @Override
+                public MavenSession getSession() {
+                    return mavenSession;
+                }
+
+                @Override
+                public MavenProject getProject() {
+                    return project;
+                }
+
+                @Override
+                public Feature getFeature() {
+                    return ctx.getFeature();
                 }
+
+                @Override
+                public ApiRegion getApiRegion() {
+                    return apiRegion;
+                }
+            };
+            if ( archiveType == ArtifactType.APIS ) {
+                p.processBinaries(pc, sources);
+            } else {
+                p.processSources(pc, sources);
             }
         }
 
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java
index 9abe9ae..d468fad 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisJarContext.java
@@ -227,7 +227,7 @@ public class ApisJarContext {
 
     private final List<ArtifactInfo> infos = new ArrayList<>();
 
-    private final ArtifactId featureId;
+    private final Feature feature;
 
     private final ApiRegions apiRegions;
 
@@ -238,7 +238,7 @@ public class ApisJarContext {
 
     public ApisJarContext(final File mainDir, final Feature feature, final ApiRegions regions) throws MojoExecutionException {
         this.config = new ApisConfiguration(feature);
-        this.featureId = feature.getId();
+        this.feature = feature;
 
         // deflated and source dirs can be shared
         this.deflatedBinDir = new File(mainDir, "deflated-bin");
@@ -252,7 +252,11 @@ public class ApisJarContext {
     }
 
     public ArtifactId getFeatureId() {
-        return featureId;
+        return feature.getId();
+    }
+
+    public Feature getFeature() {
+        return this.feature;
     }
 
     public ApiRegions getApiRegions() {
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java
index 65b4c86..8896173 100644
--- a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ApisUtil.java
@@ -30,6 +30,7 @@ import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.ServiceLoader;
 import java.util.Set;
 import java.util.TreeMap;
 
@@ -45,6 +46,7 @@ import org.apache.sling.feature.Artifact;
 import org.apache.sling.feature.ArtifactId;
 import org.apache.sling.feature.extension.apiregions.api.ApiRegion;
 import org.apache.sling.feature.maven.mojos.apis.ApisJarContext.ArtifactInfo;
+import org.apache.sling.feature.maven.mojos.apis.spi.Processor;
 import org.apache.sling.feature.maven.mojos.selection.IncludeExcludeMatcher;
 
 /**
@@ -374,4 +376,17 @@ public class ApisUtil {
         }
     }
 
+    /**
+     * Get the list of processors
+     * @return The processors - might be empty
+     */
+    public static List<Processor> getProcessors() {
+        final ServiceLoader<Processor> loader = ServiceLoader.load(Processor.class);
+
+        final List<Processor> result = new ArrayList<>();
+        for(final Processor p : loader) {
+            result.add(p);
+        }
+        return result;
+    }
 }
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java
new file mode 100644
index 0000000..ee04840
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/ArtifactType.java
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos.apis;
+
+public enum ArtifactType {
+    APIS("apis", "class", "jar"),
+    SOURCES("sources", "java", "jar"),
+    JAVADOC("javadoc", "html", "jar"),
+    DEPENDENCIES("apideps", "txt", "ref"),
+    CND("cnd", "cnd", "jar"),
+    REPORT("report", "txt", "txt");
+
+    private final String id;
+
+    private final String type;
+
+    private final String extension;
+
+    ArtifactType(final String id, final String type, final String extension) {
+        this.id = id;
+        this.type = type;
+        this.extension = extension;
+    }
+
+    public String getId() {
+        return this.id;
+    }
+
+    public String getContentType() {
+        return this.type;
+    }
+
+    public String getContentExtension() {
+        return ".".concat(this.type);
+    }
+
+    public String getExtension() {
+        return this.extension;
+    }
+}
\ No newline at end of file
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/DirectorySource.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/DirectorySource.java
new file mode 100644
index 0000000..f16e37d
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/DirectorySource.java
@@ -0,0 +1,69 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos.apis;
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.sling.feature.maven.mojos.apis.spi.Source;
+import org.codehaus.plexus.archiver.util.DefaultFileSet;
+import org.codehaus.plexus.util.DirectoryScanner;
+
+public class DirectorySource implements Source {
+
+    private final DefaultFileSet fileSet;
+
+    public DirectorySource(final DefaultFileSet set) {
+        this.fileSet = set;
+    }
+
+    @Override
+    public File getBaseDirectory() {
+        return this.fileSet.getDirectory();
+    }
+
+    @Override
+    public List<File> getFiles() {
+        final DirectoryScanner ds = new DirectoryScanner();
+        ds.setBasedir( this.fileSet.getDirectory() );
+        final String[] inc = this.fileSet.getIncludes();
+        if ( inc != null && inc.length > 0 )
+        {
+            ds.setIncludes( inc );
+        }
+        final String[] exc = this.fileSet.getExcludes();
+        if ( exc != null && exc.length > 0 )
+        {
+            ds.setExcludes( exc );
+        }
+        if ( this.fileSet.isUsingDefaultExcludes() )
+        {
+            ds.addDefaultExcludes();
+        }
+        ds.setCaseSensitive( this.fileSet.isCaseSensitive() );
+        ds.setFollowSymlinks( false );
+        ds.scan();
+
+        final List<File> result = new ArrayList<>();
+        for(final String file : ds.getIncludedFiles()) {
+            result.add(new File(this.fileSet.getDirectory(), file));
+        }
+        return result;
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/FileSource.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/FileSource.java
new file mode 100644
index 0000000..17d8d66
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/FileSource.java
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos.apis;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.sling.feature.maven.mojos.apis.spi.Source;
+
+import edu.emory.mathcs.backport.java.util.Collections;
+
+public class FileSource implements Source {
+
+    private final File directory;
+
+    private final File file;
+
+    public FileSource(final File directory, final File file) {
+        this.directory = directory;
+        this.file = file;
+    }
+
+    @Override
+    public File getBaseDirectory() {
+        return directory;
+    }
+
+    @SuppressWarnings("unchecked")
+    @Override
+    public List<File> getFiles() {
+        return Collections.singletonList(this.file);
+    }
+
+}
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/Processor.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/Processor.java
new file mode 100644
index 0000000..7b75a63
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/Processor.java
@@ -0,0 +1,47 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos.apis.spi;
+
+import java.util.List;
+
+/**
+ * A processor can be used to process the binaries or sources before they get archived.
+ */
+public interface Processor {
+
+    /**
+     * Process the sources
+     * @param ctx The context
+     * @param sources The list of sources
+     */
+    void processSources(ProcessorContext ctx, List<Source> sources);
+
+    /**
+     * Process the binaries
+     * @param ctx The context
+     * @param sources The list of sources
+     */
+    void processBinaries(ProcessorContext ctx, List<Source> sources);
+
+    /**
+     * Unique name identifying the processor
+     * @return The name
+     */
+    default String getName() {
+        return this.getClass().getName();
+    }
+}
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/ProcessorContext.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/ProcessorContext.java
new file mode 100644
index 0000000..8f33822
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/ProcessorContext.java
@@ -0,0 +1,51 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos.apis.spi;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.project.MavenProject;
+import org.apache.sling.feature.Feature;
+import org.apache.sling.feature.extension.apiregions.api.ApiRegion;
+
+/**
+ * The context is used to pass in information into a {@code Processor}
+ */
+public interface ProcessorContext {
+
+    /**
+     * Get the feature
+     * @return The feature
+     */
+    Feature getFeature();
+
+    /**
+     * Get the api region
+     * @return The api region
+     */
+    ApiRegion getApiRegion();
+
+    /**
+     * Get the project
+     * @return The project
+     */
+    MavenProject getProject();
+
+    /**
+     * Get the session
+     */
+    MavenSession getSession();
+}
diff --git a/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/Source.java b/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/Source.java
new file mode 100644
index 0000000..4116f92
--- /dev/null
+++ b/src/main/java/org/apache/sling/feature/maven/mojos/apis/spi/Source.java
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+package org.apache.sling.feature.maven.mojos.apis.spi;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * A list of source files
+ */
+public interface Source {
+
+    /**
+     * The base directory - all files are in the directory sub tree of this directory
+     * @return The base directory
+     */
+    File getBaseDirectory();
+
+    /**
+     * The list of files
+     * @return The list of files
+     */
+    List<File> getFiles();
+}