You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2015/04/15 10:54:43 UTC

karaf git commit: [KARAF-3653] Karaf can't be started in offline mode

Repository: karaf
Updated Branches:
  refs/heads/master b24cd0f87 -> c410e1db4


[KARAF-3653] Karaf can't be started in offline mode

Use symbolic links for libraries and store the original artifacts in the system folder.
The assemblies can support those symbolic names, but disable this behaviour by default as it’s not well supported on all platforms.


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/c410e1db
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/c410e1db
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/c410e1db

Branch: refs/heads/master
Commit: c410e1db4ca3a68c47ccbef27e8a606b28df0381
Parents: b24cd0f
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Wed Apr 15 10:49:09 2015 +0200
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Wed Apr 15 10:54:27 2015 +0200

----------------------------------------------------------------------
 pom.xml                                         |   6 +
 .../apache/karaf/profile/assembly/Builder.java  |  13 +-
 tooling/karaf-maven-plugin/pom.xml              |   8 +-
 .../tooling/instances/CreateArchiveMojo.java    | 211 ++++++++++---------
 4 files changed, 137 insertions(+), 101 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/c410e1db/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index fe90d7e..61a0ee2 100644
--- a/pom.xml
+++ b/pom.xml
@@ -133,6 +133,7 @@
         <cglib2.version>2.2.0</cglib2.version>
         <commons-beanutils.version>1.9.2</commons-beanutils.version>
         <commons-codec.version>1.10</commons-codec.version>
+        <commons-compress.version>1.8.1</commons-compress.version>
         <commons-collections.version>3.2.1</commons-collections.version>
         <commons-dbcp.version>1.4</commons-dbcp.version>
         <commons-fileupload.version>1.3.1</commons-fileupload.version>
@@ -1818,6 +1819,11 @@
                 <artifactId>commons-io</artifactId>
                 <version>${commons-io.version}</version>
             </dependency>
+            <dependency>
+                <groupId>org.apache.commons</groupId>
+                <artifactId>commons-compress</artifactId>
+                <version>${commons-compress.version}</version>
+            </dependency>
 
             <dependency>
                 <groupId>org.apache.servicemix.bundles</groupId>

http://git-wip-us.apache.org/repos/asf/karaf/blob/c410e1db/profile/src/main/java/org/apache/karaf/profile/assembly/Builder.java
----------------------------------------------------------------------
diff --git a/profile/src/main/java/org/apache/karaf/profile/assembly/Builder.java b/profile/src/main/java/org/apache/karaf/profile/assembly/Builder.java
index 319d455..21b0a44 100644
--- a/profile/src/main/java/org/apache/karaf/profile/assembly/Builder.java
+++ b/profile/src/main/java/org/apache/karaf/profile/assembly/Builder.java
@@ -617,8 +617,17 @@ public class Builder {
                     synchronized (provider) {
                         Path input = provider.getFile().toPath();
                         String name = filename != null ? filename : input.getFileName().toString();
-                        Path output = homeDirectory.resolve(path).resolve(name);
-                        Files.copy(input, output, StandardCopyOption.REPLACE_EXISTING);
+                        if (provider.getUrl().startsWith("mvn:")) {
+                            String mvnPath = Parser.pathFromMaven(provider.getUrl());
+                            Path sysOutput = systemDirectory.resolve(mvnPath);
+                            Files.createDirectories(sysOutput.getParent());
+                            Files.copy(input, sysOutput, StandardCopyOption.REPLACE_EXISTING);
+                            Path libOutput = homeDirectory.resolve(path).resolve(name);
+                            Files.createSymbolicLink(libOutput, libOutput.getParent().relativize(sysOutput));
+                        } else {
+                            Path output = homeDirectory.resolve(path).resolve(name);
+                            Files.copy(input, output, StandardCopyOption.REPLACE_EXISTING);
+                        }
                     }
                     boolean export = Boolean.parseBoolean(clause.getDirective(LIBRARY_CLAUSE_EXPORT));
                     boolean delegate = Boolean.parseBoolean(clause.getDirective(LIBRARY_CLAUSE_DELEGATE));

http://git-wip-us.apache.org/repos/asf/karaf/blob/c410e1db/tooling/karaf-maven-plugin/pom.xml
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/pom.xml b/tooling/karaf-maven-plugin/pom.xml
index 39af7ba..3c33770 100644
--- a/tooling/karaf-maven-plugin/pom.xml
+++ b/tooling/karaf-maven-plugin/pom.xml
@@ -176,14 +176,14 @@
             <artifactId>org.apache.karaf.kar.core</artifactId>
         </dependency>
         <dependency>
-            <groupId>org.apache.servicemix.bundles</groupId>
-            <artifactId>org.apache.servicemix.bundles.ant</artifactId>
-        </dependency>
-        <dependency>
             <groupId>org.apache.karaf.shell</groupId>
             <artifactId>org.apache.karaf.shell.console</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-compress</artifactId>
+        </dependency>
+        <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
             <scope>compile</scope>

http://git-wip-us.apache.org/repos/asf/karaf/blob/c410e1db/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/instances/CreateArchiveMojo.java
----------------------------------------------------------------------
diff --git a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/instances/CreateArchiveMojo.java b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/instances/CreateArchiveMojo.java
index f8d2082..98d98a1 100644
--- a/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/instances/CreateArchiveMojo.java
+++ b/tooling/karaf-maven-plugin/src/main/java/org/apache/karaf/tooling/instances/CreateArchiveMojo.java
@@ -18,24 +18,29 @@
  */
 package org.apache.karaf.tooling.instances;
 
+import java.io.BufferedOutputStream;
 import java.io.File;
 import java.io.IOException;
-
+import java.io.OutputStream;
+import java.nio.file.DirectoryStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
+import org.apache.commons.compress.archivers.tar.TarConstants;
+import org.apache.commons.compress.archivers.zip.UnixStat;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
 import org.apache.karaf.tooling.utils.MojoSupport;
-import org.apache.maven.model.Resource;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
 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.tools.ant.Project;
-import org.apache.tools.ant.taskdefs.MatchingTask;
-import org.apache.tools.ant.taskdefs.Tar;
-import org.apache.tools.ant.taskdefs.Zip;
-import org.apache.tools.ant.types.TarFileSet;
-import org.apache.tools.ant.types.ZipFileSet;
-import org.apache.maven.artifact.Artifact;
-import org.apache.maven.plugin.MojoExecutionException;
-import org.apache.maven.plugin.MojoFailureException;
 
 /**
  * Package a server archive from an assembled server
@@ -73,6 +78,16 @@ public class CreateArchiveMojo extends MojoSupport {
     @Parameter
     private boolean archiveZip = true;
 
+    /**
+     * use symbolic links in tar.gz or zip archives
+     *
+     * Symbolic links are not very well supported by windows Platform.
+     * At least, is does not work on WinXP + NTFS, so do not include them
+     * for now. So the default is false.
+     */
+    @Parameter
+    private boolean useSymLinks = false;
+
     public void execute() throws MojoExecutionException, MojoFailureException {
         getLog().debug("Setting artifact file: " + targetFile);
         org.apache.maven.artifact.Artifact artifact = project.getArtifact();
@@ -106,100 +121,106 @@ public class CreateArchiveMojo extends MojoSupport {
            serverName = artifact.getArtifactId() + "-" + artifact.getVersion();
         }
         dest = new File(dest, serverName + "." + artifact.getType());
-        Project project = new Project();
-        MatchingTask archiver;
+
         if ("tar.gz".equals(artifact.getType())) {
-            Tar tar = new Tar();
-            Tar.TarCompressionMethod tarCompressionMethod = new Tar.TarCompressionMethod();
-            tarCompressionMethod.setValue("gzip");
-            tar.setCompression(tarCompressionMethod);
-            Tar.TarLongFileMode fileMode = new Tar.TarLongFileMode();
-            fileMode.setValue(Tar.TarLongFileMode.GNU);
-            tar.setLongfile(fileMode);
-            tar.setDestFile(dest);
-            TarFileSet rc = new TarFileSet();
-            rc.setDir(source);
-            rc.setPrefix(serverName);
-            rc.setProject(project);
-            rc.setExcludes("bin/");
-            tar.add(rc);
-
-            rc = new TarFileSet();
-            rc.setDir(source);
-            rc.setPrefix(serverName);
-            rc.setProject(project);
-            rc.setIncludes("bin/");
-            rc.setExcludes("bin/*.bat");
-            rc.setFileMode("755");
-            tar.add(rc);
-
-            rc = new TarFileSet();
-            rc.setDir(source);
-            rc.setPrefix(serverName);
-            rc.setProject(project);
-            rc.setIncludes("bin/*.bat");
-            tar.add(rc);
-
-            for (Resource resource: this.project.getResources()) {
-                File resourceFile = new File(resource.getDirectory());
-                if (resourceFile.exists()) {
-                    rc = new TarFileSet();
-                    rc.setPrefix(serverName);
-                    rc.setProject(project);
-                    rc.setDir(resourceFile);
-                    rc.appendIncludes(resource.getIncludes().toArray(new String[0]));
-                    rc.appendExcludes(resource.getExcludes().toArray(new String[0]));
-                    tar.add(rc);
+            try (
+                    OutputStream fOut = Files.newOutputStream(dest.toPath());
+                    OutputStream bOut = new BufferedOutputStream(fOut);
+                    OutputStream gzOut = new GzipCompressorOutputStream(bOut);
+                    TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);
+                    DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())
+
+            ) {
+                tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
+                tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
+                for (Path child : children) {
+                    addFileToTarGz(tOut, child, serverName + "/");
                 }
             }
-
-            archiver = tar;
         } else if ("zip".equals(artifact.getType())) {
-            Zip zip = new Zip();
-            zip.setDestFile(dest);
-            ZipFileSet fs = new ZipFileSet();
-            fs.setDir(source);
-            fs.setPrefix(serverName);
-            fs.setProject(project);
-            fs.setExcludes("bin/");
-            zip.addFileset(fs);
-
-            fs = new ZipFileSet();
-            fs.setDir(source);
-            fs.setPrefix(serverName);
-            fs.setProject(project);
-            fs.setIncludes("bin/");
-            fs.setExcludes("bin/*.bat");
-            fs.setFileMode("755");
-            zip.add(fs);
-
-            fs = new ZipFileSet();
-            fs.setDir(source);
-            fs.setPrefix(serverName);
-            fs.setProject(project);
-            fs.setIncludes("bin/*.bat");
-            zip.add(fs);
-
-            for (Resource resource: this.project.getResources()) {
-                File resourceFile = new File(resource.getDirectory());
-                if (resourceFile.exists()) {
-                    fs = new ZipFileSet();
-                    fs.setPrefix(serverName);
-                    fs.setProject(project);
-                    fs.setDir(resourceFile);
-                    fs.appendIncludes(resource.getIncludes().toArray(new String[0]));
-                    fs.appendExcludes(resource.getExcludes().toArray(new String[0]));
-                    zip.add(fs);
+            try (
+                    OutputStream fOut = Files.newOutputStream(dest.toPath());
+                    OutputStream bOut = new BufferedOutputStream(fOut);
+                    ZipArchiveOutputStream tOut = new ZipArchiveOutputStream(bOut);
+                    DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())
+
+            ) {
+                for (Path child : children) {
+                    addFileToZip(tOut, child, serverName + "/");
                 }
             }
-
-            archiver = zip;
         } else {
             throw new IllegalArgumentException("Unknown target type: " + artifact.getType());
         }
-        archiver.setProject(project);
-        archiver.execute();
+
         return dest;
     }
 
+    private void addFileToTarGz(TarArchiveOutputStream tOut, Path f, String base) throws IOException {
+        if (Files.isDirectory(f)) {
+            String entryName = base + f.getFileName().toString() + "/";
+            TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
+            tOut.putArchiveEntry(tarEntry);
+            tOut.closeArchiveEntry();
+            try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) {
+                for (Path child : children) {
+                    addFileToTarGz(tOut, child, entryName);
+                }
+            }
+        } else if (useSymLinks && Files.isSymbolicLink(f)) {
+            String entryName = base + f.getFileName().toString();
+            TarArchiveEntry tarEntry = new TarArchiveEntry(entryName, TarConstants.LF_SYMLINK);
+            tarEntry.setLinkName(Files.readSymbolicLink(f).toString());
+            tOut.putArchiveEntry(tarEntry);
+            tOut.closeArchiveEntry();
+        }  else {
+            String entryName = base + f.getFileName().toString();
+            TarArchiveEntry tarEntry = new TarArchiveEntry(entryName);
+            tarEntry.setSize(Files.size(f));
+            if (entryName.contains("/bin/")) {
+                tarEntry.setMode(0755);
+                if (entryName.endsWith(".bat")) {
+                    return;
+                }
+            }
+            tOut.putArchiveEntry(tarEntry);
+            Files.copy(f, tOut);
+            tOut.closeArchiveEntry();
+        }
+    }
+
+    private void addFileToZip(ZipArchiveOutputStream tOut, Path f, String base) throws IOException {
+        if (Files.isDirectory(f)) {
+            String entryName = base + f.getFileName().toString() + "/";
+            ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
+            tOut.putArchiveEntry(zipEntry);
+            tOut.closeArchiveEntry();
+            try (DirectoryStream<Path> children = Files.newDirectoryStream(f)) {
+                for (Path child : children) {
+                    addFileToZip(tOut, child, entryName);
+                }
+            }
+        } else if (useSymLinks && Files.isSymbolicLink(f)) {
+            String entryName = base + f.getFileName().toString();
+            ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
+            zipEntry.setUnixMode(UnixStat.LINK_FLAG | UnixStat.DEFAULT_FILE_PERM);
+            tOut.putArchiveEntry(zipEntry);
+            tOut.write(Files.readSymbolicLink(f).toString().getBytes());
+            tOut.closeArchiveEntry();
+        }  else {
+            String entryName = base + f.getFileName().toString();
+            ZipArchiveEntry zipEntry = new ZipArchiveEntry(entryName);
+            zipEntry.setSize(Files.size(f));
+            if (entryName.contains("/bin/")) {
+                if (!entryName.endsWith(".bat")) {
+                    return;
+                }
+                zipEntry.setUnixMode(0755);
+            }
+            tOut.putArchiveEntry(zipEntry);
+            Files.copy(f, tOut);
+            tOut.closeArchiveEntry();
+        }
+    }
+
 }