Search in sources :

Example 1 with ResourceIterator

use of org.codehaus.plexus.archiver.ResourceIterator in project sofa-ark by alipay.

the class ArkPluginMojo method getLibFileArchiveEntries.

private List<ArchiveEntry> getLibFileArchiveEntries(Archiver archiver) {
    ResourceIterator resourceIterator = archiver.getResources();
    List<ArchiveEntry> result = new ArrayList<>();
    while (resourceIterator.hasNext()) {
        ArchiveEntry archiveEntry = resourceIterator.next();
        if (archiveEntry.getType() == ArchiveEntry.FILE) {
            String name = archiveEntry.getName();
            if (name.startsWith("lib/") && name.endsWith(".jar")) {
                if (name.indexOf("/") == name.lastIndexOf("/") && name.contains("/")) {
                    result.add(archiveEntry);
                }
            }
        }
    }
    return result;
}
Also used : ArchiveEntry(org.codehaus.plexus.archiver.ArchiveEntry) ResourceIterator(org.codehaus.plexus.archiver.ResourceIterator)

Example 2 with ResourceIterator

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

the class AllFilesExecCustomizer method customize.

@Override
public TarArchiver customize(TarArchiver archiver) throws IOException {
    log.warn("/--------------------- SECURITY WARNING ---------------------\\");
    log.warn("|You are building a Docker image with normalized permissions.|");
    log.warn("|All files and directories added to build context will have  |");
    log.warn("|'-rwxr-xr-x' permissions. It is recommended to double check |");
    log.warn("|and reset permissions for sensitive files and directories.  |");
    log.warn("\\------------------------------------------------------------/");
    TarArchiver newArchiver = new TarArchiver();
    newArchiver.setDestFile(archiver.getDestFile());
    newArchiver.setLongfile(TarLongFileMode.posix);
    ResourceIterator resources = archiver.getResources();
    while (resources.hasNext()) {
        ArchiveEntry ae = resources.next();
        String fileName = ae.getName();
        PlexusIoResource resource = ae.getResource();
        String name = StringUtils.replace(fileName, File.separatorChar, '/');
        // See docker source:
        // https://github.com/docker/docker/blob/3d13fddd2bc4d679f0eaa68b0be877e5a816ad53/pkg/archive/archive_windows.go#L45
        int mode = ae.getMode() & 0777;
        int newMode = mode;
        newMode &= 0755;
        newMode |= 0111;
        if (newMode != mode) {
            log.debug("Changing permissions of '%s' from %o to %o.", name, mode, newMode);
        }
        newArchiver.addResource(resource, name, newMode);
    }
    archiver = newArchiver;
    return archiver;
}
Also used : TarArchiver(org.codehaus.plexus.archiver.tar.TarArchiver) PlexusIoResource(org.codehaus.plexus.components.io.resources.PlexusIoResource) ArchiveEntry(org.codehaus.plexus.archiver.ArchiveEntry) ResourceIterator(org.codehaus.plexus.archiver.ResourceIterator)

Example 3 with ResourceIterator

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

the class DockerAssemblyManagerTest method testArchiveCreationNoDockerfileWithExecutableAssemblies.

@Test
public void testArchiveCreationNoDockerfileWithExecutableAssemblies(@Mocked final PlexusIoResource resource, @Mocked final ArchiveEntry archiveEntry, @Mocked final TarArchiver tarArchiver, @Injectable final Logger logger) throws MojoExecutionException, NoSuchArchiverException, IOException {
    MojoParameters mojoParams = mockMojoParams(mockMavenProject());
    FileItem testFile = new FileItem();
    testFile.setDestName("test.txt");
    testFile.setSource("test.in");
    Assembly testAssembly = new Assembly();
    testAssembly.addFile(testFile);
    new Expectations() {

        {
            archiveEntry.getName();
            result = "test";
            archiveEntry.getMode();
            result = 0644;
            archiveEntry.getResource();
            result = resource;
            tarArchiver.getResources();
            result = new ResourceIterator() {

                boolean consumed = false;

                @Override
                public boolean hasNext() {
                    return !consumed;
                }

                @Override
                public ArchiveEntry next() {
                    if (consumed) {
                        return null;
                    }
                    consumed = true;
                    return archiveEntry;
                }
            };
        }
    };
    BuildImageConfiguration buildImageConfiguration = new BuildImageConfiguration.Builder().assemblies(Collections.singletonList(new AssemblyConfiguration.Builder().name("first").mode(AssemblyMode.tar.name()).assemblyDef(testAssembly).permissions(AssemblyConfiguration.PermissionMode.exec.name()).build())).build();
    buildImageConfiguration.initAndValidate(logger);
    File tarArchive = assemblyManager.createDockerTarArchive("test_image", mojoParams, buildImageConfiguration, logger, null);
    assertNotNull(tarArchive);
    new Verifications() {

        {
            archiverManager.getArchiver("tar");
            times = 1;
            tarArchiver.addFile(new File("target/test_image/build/Dockerfile"), "Dockerfile");
            List<ArchivedFileSet> archivedFileSets = new ArrayList<>();
            tarArchiver.addArchivedFileSet(withCapture(archivedFileSets));
            assertEquals(1, archivedFileSets.size());
            assertEquals(new File("target/test_image/build/first.tar"), archivedFileSets.get(0).getArchive());
            assertEquals("first/", archivedFileSets.get(0).getPrefix());
            tarArchiver.addResource((PlexusIoResource) any, "test", 0755);
        }
    };
}
Also used : Expectations(mockit.Expectations) ArrayList(java.util.ArrayList) ArchiveEntry(org.codehaus.plexus.archiver.ArchiveEntry) ArchivedFileSet(org.codehaus.plexus.archiver.ArchivedFileSet) Verifications(mockit.Verifications) BuildImageConfiguration(io.fabric8.maven.docker.config.BuildImageConfiguration) FileItem(org.apache.maven.plugins.assembly.model.FileItem) MojoParameters(io.fabric8.maven.docker.util.MojoParameters) ResourceIterator(org.codehaus.plexus.archiver.ResourceIterator) File(java.io.File) Assembly(org.apache.maven.plugins.assembly.model.Assembly) Test(org.junit.Test)

Aggregations

ArchiveEntry (org.codehaus.plexus.archiver.ArchiveEntry)3 ResourceIterator (org.codehaus.plexus.archiver.ResourceIterator)3 BuildImageConfiguration (io.fabric8.maven.docker.config.BuildImageConfiguration)1 MojoParameters (io.fabric8.maven.docker.util.MojoParameters)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Expectations (mockit.Expectations)1 Verifications (mockit.Verifications)1 Assembly (org.apache.maven.plugins.assembly.model.Assembly)1 FileItem (org.apache.maven.plugins.assembly.model.FileItem)1 ArchivedFileSet (org.codehaus.plexus.archiver.ArchivedFileSet)1 TarArchiver (org.codehaus.plexus.archiver.tar.TarArchiver)1 PlexusIoResource (org.codehaus.plexus.components.io.resources.PlexusIoResource)1 Test (org.junit.Test)1