Search in sources :

Example 6 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManager method createBuildTarBall.

// Create final tar-ball to be used for building the archive to send to the Docker daemon
private File createBuildTarBall(BuildDirs buildDirs, List<ArchiverCustomizer> archiverCustomizers, AssemblyConfiguration assemblyConfig, ArchiveCompression compression) throws MojoExecutionException {
    File archive = new File(buildDirs.getTemporaryRootDirectory(), "docker-build." + compression.getFileSuffix());
    try {
        TarArchiver archiver = createBuildArchiver(buildDirs.getOutputDirectory(), archive, assemblyConfig);
        for (ArchiverCustomizer customizer : archiverCustomizers) {
            if (customizer != null) {
                archiver = customizer.customize(archiver);
            }
        }
        archiver.setCompression(compression.getTarCompressionMethod());
        archiver.createArchive();
        return archive;
    } catch (NoSuchArchiverException e) {
        throw new MojoExecutionException("No archiver for type 'tar' found", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Cannot create archive " + archive, e);
    }
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) IOException(java.io.IOException) File(java.io.File) NoSuchArchiverException(org.codehaus.plexus.archiver.manager.NoSuchArchiverException)

Example 7 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project docker-maven-plugin by fabric8io.

the class DockerAssemblyManager method createBuildArchiver.

private TarArchiver createBuildArchiver(File outputDir, File archive, AssemblyConfiguration assemblyConfig) throws NoSuchArchiverException {
    TarArchiver archiver = (TarArchiver) archiverManager.getArchiver("tar");
    archiver.setLongfile(TarLongFileMode.posix);
    AssemblyMode mode = assemblyConfig != null ? assemblyConfig.getMode() : null;
    if (mode != null && mode.isArchive()) {
        DefaultArchivedFileSet archiveSet = DefaultArchivedFileSet.archivedFileSet(new File(outputDir, assemblyConfig.getName() + "." + mode.getExtension()));
        archiveSet.setPrefix(assemblyConfig.getName() + "/");
        archiveSet.setIncludingEmptyDirectories(true);
        archiveSet.setUsingDefaultExcludes(false);
        archiver.addArchivedFileSet(archiveSet);
    } else {
        DefaultFileSet fileSet = DefaultFileSet.fileSet(outputDir);
        fileSet.setUsingDefaultExcludes(false);
        archiver.addFileSet(fileSet);
    }
    archiver.setDestFile(archive);
    return archiver;
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) DefaultFileSet(org.codehaus.plexus.archiver.util.DefaultFileSet) DefaultArchivedFileSet(org.codehaus.plexus.archiver.util.DefaultArchivedFileSet) AssemblyMode(io.fabric8.maven.docker.config.AssemblyMode) File(java.io.File)

Example 8 with TarArchiver

use of org.codehaus.plexus.archiver.tar.TarArchiver in project fabric8 by fabric8io.

the class BuildMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    File libDir = new File(assembly, "lib");
    libDir.mkdirs();
    File binDir = new File(assembly, "bin");
    binDir.mkdirs();
    ArrayList<String> classpath = new ArrayList<String>();
    // get sets of dependencies
    ArrayList<Artifact> artifacts = null;
    try {
        artifacts = collectClassPath();
    } catch (DependencyGraphBuilderException e) {
        throw new MojoExecutionException("Could not get classpath", e);
    }
    getLog().debug("Classpath for " + scope + ":\n" + artifactsToString(artifacts));
    // Lets first copy this project's artifact.
    if (project.getArtifact().getFile() != null) {
        File target = new File(libDir, project.getArtifact().getFile().getName());
        classpath.add(target.getName());
        try {
            FileUtils.copyFile(project.getArtifact().getFile(), target);
        } catch (IOException e) {
            throw new MojoExecutionException("Could not copy artifact to lib dir", e);
        }
    }
    // Artifacts in this map point to resolved files.
    // project.getArtifactMap() doesn't include type or classifier in map key so we need to roll our own...
    Map artifactMap = getArtifactMap();
    // Lets then copy the it's dependencies.
    for (Artifact x : artifacts) {
        // x is not resolved, so lets look it up in the map.
        Artifact artifact = (Artifact) artifactMap.get(versionlessKey(x));
        // which should be treated as equivalent so we have to roll our own equals...
        if (artifact == null || artifact.getFile() == null || !artifactEquals(artifact, x)) {
            continue;
        }
        getLog().debug("Copying " + artifact.toString());
        File file = artifact.getFile().getAbsoluteFile();
        try {
            File target = new File(libDir, file.getName());
            // just in case we run into an lib name collision, lets
            // find a non-colliding target name
            int dupCounter = 1;
            while (classpath.contains(target.getName())) {
                target = new File(libDir, "dup" + dupCounter + "-" + file.getName());
                dupCounter++;
            }
            classpath.add(target.getName());
            FileUtils.copyFile(artifact.getFile(), target);
        } catch (IOException e) {
            throw new MojoExecutionException("Could not copy artifact to lib dir", e);
        }
    }
    // Finally lets write the classpath.
    try {
        String classpathTxt = StringUtils.join(classpath.iterator(), "\n") + "\n";
        FileUtils.fileWrite(new File(libDir, "classpath"), classpathTxt);
    } catch (IOException e) {
        throw new MojoExecutionException("Could create the classpath file", e);
    }
    HashMap<String, String> interpolations = new HashMap<String, String>();
    // Be sure that an empty string is replaced when no main class is given
    interpolations.put("hawtapp.mvn.main.property", javaMainClass != null ? javaMainClass : "");
    File targetRun = new File(binDir, "run.sh");
    copyResource("bin/run.sh", targetRun, interpolations, true);
    chmodExecutable(targetRun);
    File targetRunCmd = new File(binDir, "run.cmd");
    copyResource("bin/run.cmd", targetRunCmd, interpolations, false);
    if (source != null && source.exists()) {
        try {
            FileUtils.copyDirectoryStructure(source, assembly);
        } catch (IOException e) {
            throw new MojoExecutionException("Could copy the hawt-app resources", e);
        }
    }
    Archiver archiver;
    String archiveExtension;
    if (archive.getName().endsWith(".tar")) {
        archiver = tarArchiver;
        archiveExtension = "tar";
    } else if (archive.getName().endsWith(".tar.gz")) {
        ((TarArchiver) tarArchiver).setCompression(TarArchiver.TarCompressionMethod.gzip);
        archiver = tarArchiver;
        archiveExtension = "tar.gz";
    } else if (archive.getName().endsWith(".zip")) {
        archiver = zipArchiver;
        archiveExtension = "zip";
    } else {
        throw new MojoExecutionException("Invalid archive extension.  Should be zip | tar | tar.gz");
    }
    archiver.setDestFile(archive);
    archiver.addFileSet(fileSet(assembly).prefixed(archivePrefix).includeExclude(null, new String[] { "bin/*" }).includeEmptyDirs(true));
    archiver.setFileMode(0755);
    archiver.addFileSet(fileSet(assembly).prefixed(archivePrefix).includeExclude(new String[] { "bin/*" }, null).includeEmptyDirs(true));
    try {
        archiver.createArchive();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not create the " + archive + " file", e);
    }
    projectHelper.attachArtifact(project, archiveExtension, archiveClassifier, archive);
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DependencyGraphBuilderException(org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Artifact(org.apache.maven.artifact.Artifact) File(java.io.File) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) Archiver(org.codehaus.plexus.archiver.Archiver)

Aggregations

TarArchiver (org.codehaus.plexus.archiver.tar.TarArchiver)8 File (java.io.File)5 IOException (java.io.IOException)5 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 NoSuchArchiverException (org.codehaus.plexus.archiver.manager.NoSuchArchiverException)2 DefaultFileSet (org.codehaus.plexus.archiver.util.DefaultFileSet)2 Fabric8ServiceException (io.fabric8.maven.core.service.Fabric8ServiceException)1 ArchiverCustomizer (io.fabric8.maven.docker.assembly.ArchiverCustomizer)1 AssemblyConfiguration (io.fabric8.maven.docker.config.AssemblyConfiguration)1 AssemblyMode (io.fabric8.maven.docker.config.AssemblyMode)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Artifact (org.apache.maven.artifact.Artifact)1 DependencyGraphBuilderException (org.apache.maven.shared.dependency.graph.DependencyGraphBuilderException)1 ArchiveEntry (org.codehaus.plexus.archiver.ArchiveEntry)1 Archiver (org.codehaus.plexus.archiver.Archiver)1