Example usage for org.springframework.boot.loader.tools Library Library

List of usage examples for org.springframework.boot.loader.tools Library Library

Introduction

In this page you can find the example usage for org.springframework.boot.loader.tools Library Library.

Prototype

public Library(File file, LibraryScope scope) 

Source Link

Document

Create a new Library .

Usage

From source file:org.eclipse.cft.server.standalone.ui.internal.application.JarArchivingUIHandler.java

protected void bootRepackage(final IPackageFragmentRoot[] roots, File packagedFile) throws CoreException {
    Repackager bootRepackager = new Repackager(packagedFile);
    try {//  w  ww . j  a  va  2  s  . c  o m
        bootRepackager.repackage(new Libraries() {

            public void doWithLibraries(LibraryCallback callBack) throws IOException {
                for (IPackageFragmentRoot root : roots) {

                    if (root.isArchive()) {

                        File rootFile = new File(root.getPath().toOSString());
                        if (rootFile.exists()) {
                            callBack.library(new Library(rootFile, LibraryScope.COMPILE));
                        }
                    }
                }
            }
        });
    } catch (IOException e) {
        errorHandler.handleApplicationDeploymentFailure(
                NLS.bind(Messages.JavaCloudFoundryArchiver_ERROR_REPACKAGE_SPRING, e.getMessage()));
    }
}

From source file:com.hurence.logisland.plugin.PluginManager.java

private static void installPlugin(String artifact, String logislandHome) {
    Optional<ModuleInfo> moduleInfo = findPluginMeta().entrySet().stream()
            .filter(e -> artifact.equals(e.getKey().getArtifact())).map(Map.Entry::getKey).findFirst();
    if (moduleInfo.isPresent()) {
        System.err//from   w  w w  .  j  av  a 2  s.co  m
                .println("A component already matches the artifact " + artifact + ". Please remove it first.");
        System.exit(-1);
    }

    try {

        IvySettings settings = new IvySettings();
        settings.load(new File(logislandHome, "conf/ivy.xml"));

        Ivy ivy = Ivy.newInstance(settings);
        ivy.bind();

        System.out.println("\nDownloading dependencies. Please hold on...\n");

        String parts[] = Arrays.stream(artifact.split(":")).map(String::trim).toArray(a -> new String[a]);
        if (parts.length != 3) {
            throw new IllegalArgumentException(
                    "Unrecognized artifact format. It should be groupId:artifactId:version");
        }
        ModuleRevisionId revisionId = new ModuleRevisionId(new ModuleId(parts[0], parts[1]), parts[2]);
        Set<ArtifactDownloadReport> toBePackaged = downloadArtifacts(ivy, revisionId,
                new String[] { "default", "compile", "runtime" });

        ArtifactDownloadReport artifactJar = toBePackaged.stream()
                .filter(a -> a.getArtifact().getModuleRevisionId().equals(revisionId)).findFirst()
                .orElseThrow(() -> new IllegalStateException("Unable to find artifact " + artifact
                        + ". Please check the name is correct and the repositories on ivy.xml are correctly configured"));

        Manifest manifest = new JarFile(artifactJar.getLocalFile()).getManifest();
        File libDir = new File(logislandHome, "lib");

        if (manifest.getMainAttributes().containsKey(ManifestAttributes.MODULE_ARTIFACT)) {
            org.apache.commons.io.FileUtils.copyFileToDirectory(artifactJar.getLocalFile(), libDir);
            //we have a logisland plugin. Just copy it
            System.out.println(String.format("Found logisland plugin %s version %s\n" + "It will provide:",
                    manifest.getMainAttributes().getValue(ManifestAttributes.MODULE_NAME),
                    manifest.getMainAttributes().getValue(ManifestAttributes.MODULE_VERSION)));
            Arrays.stream(manifest.getMainAttributes().getValue(ManifestAttributes.MODULE_EXPORTS).split(","))
                    .map(String::trim).forEach(s -> System.out.println("\t" + s));

        } else {
            System.out.println("Repackaging artifact and its dependencies");
            Set<ArtifactDownloadReport> environment = downloadArtifacts(ivy, revisionId,
                    new String[] { "provided" });
            Set<ArtifactDownloadReport> excluded = toBePackaged.stream()
                    .filter(adr -> excludeGroupIds.stream()
                            .anyMatch(s -> s.matches(adr.getArtifact().getModuleRevisionId().getOrganisation()))
                            || excludedArtifactsId.stream().anyMatch(
                                    s -> s.matches(adr.getArtifact().getModuleRevisionId().getName())))
                    .collect(Collectors.toSet());

            toBePackaged.removeAll(excluded);
            environment.addAll(excluded);

            Repackager rep = new Repackager(artifactJar.getLocalFile(), new LogislandPluginLayoutFactory());
            rep.setMainClass("");
            File destFile = new File(libDir, "logisland-component-" + artifactJar.getLocalFile().getName());
            rep.repackage(destFile, callback -> toBePackaged.stream().filter(adr -> adr.getLocalFile() != null)
                    .filter(adr -> !adr.getArtifact().getModuleRevisionId().equals(revisionId))
                    .map(adr -> new Library(adr.getLocalFile(), LibraryScope.COMPILE)).forEach(library -> {
                        try {
                            callback.library(library);
                        } catch (IOException e) {
                            throw new UncheckedIOException(e);
                        }
                    }));
            Thread.currentThread().setContextClassLoader(new URLClassLoader(
                    environment.stream().filter(adr -> adr.getLocalFile() != null).map(adr -> {
                        try {
                            return adr.getLocalFile().toURI().toURL();
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }).toArray(a -> new URL[a]), Thread.currentThread().getContextClassLoader()));
            //now clean up package and write the manifest
            String newArtifact = "com.hurence.logisland.repackaged:" + parts[1] + ":" + parts[2];
            LogislandRepackager.execute(destFile.getAbsolutePath(), "BOOT-INF/lib-provided", parts[2],
                    newArtifact, "Logisland Component for " + artifact, "Logisland Component for " + artifact,
                    new String[] { "org.apache.kafka.*" }, new String[0],
                    "org.apache.kafka.connect.connector.Connector");
        }
        System.out.println("Install done!");
    } catch (Exception e) {
        System.err.println("Unable to install artifact " + artifact);
        e.printStackTrace();
        System.exit(-1);
    }

}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void libraries() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class, JAN_1_1985);
    File libJarFile = libJar.getFile();
    File libJarFileToUnpack = libJar.getFile();
    File libNonJarFile = this.temporaryFolder.newFile();
    FileCopyUtils.copy(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 }, libNonJarFile);
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    this.testJarFile.addFile("BOOT-INF/lib/" + libJarFileToUnpack.getName(), libJarFileToUnpack);
    File file = this.testJarFile.getFile();
    libJarFile.setLastModified(JAN_1_1980);
    Repackager repackager = new Repackager(file);
    repackager.repackage((callback) -> {
        callback.library(new Library(libJarFile, LibraryScope.COMPILE));
        callback.library(new Library(libJarFileToUnpack, LibraryScope.COMPILE, true));
        callback.library(new Library(libNonJarFile, LibraryScope.COMPILE));
    });/*from  w  w  w.jav  a 2s .c o  m*/
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFile.getName())).isTrue();
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName())).isTrue();
    assertThat(hasEntry(file, "BOOT-INF/lib/" + libNonJarFile.getName())).isFalse();
    JarEntry entry = getEntry(file, "BOOT-INF/lib/" + libJarFile.getName());
    assertThat(entry.getTime()).isEqualTo(JAN_1_1985);
    entry = getEntry(file, "BOOT-INF/lib/" + libJarFileToUnpack.getName());
    assertThat(entry.getComment()).startsWith("UNPACK:");
    assertThat(entry.getComment().length()).isEqualTo(47);
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void customLayout() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    Layout layout = mock(Layout.class);
    LibraryScope scope = mock(LibraryScope.class);
    given(layout.getLauncherClassName()).willReturn("testLauncher");
    given(layout.getLibraryDestination(anyString(), eq(scope))).willReturn("test/");
    given(layout.getLibraryDestination(anyString(), eq(LibraryScope.COMPILE))).willReturn("test-lib/");
    repackager.setLayout(layout);//from  www . ja v a2  s .c o m
    repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
    assertThat(hasEntry(file, "test/" + libJarFile.getName())).isTrue();
    assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib")).isEqualTo("test-lib/");
    assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")).isEqualTo("testLauncher");
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void customLayoutNoBootLib() throws Exception {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    Layout layout = mock(Layout.class);
    LibraryScope scope = mock(LibraryScope.class);
    given(layout.getLauncherClassName()).willReturn("testLauncher");
    repackager.setLayout(layout);/*  ww  w .j ava2 s .co  m*/
    repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
    assertThat(getManifest(file).getMainAttributes().getValue("Spring-Boot-Lib")).isNull();
    assertThat(getManifest(file).getMainAttributes().getValue("Main-Class")).isEqualTo("testLauncher");
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void dontRecompressZips() throws Exception {
    TestJarFile nested = new TestJarFile(this.temporaryFolder);
    nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File nestedFile = nested.getFile();
    this.testJarFile.addFile("test/nested.jar", nestedFile);
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage((callback) -> callback.library(new Library(nestedFile, LibraryScope.COMPILE)));

    try (JarFile jarFile = new JarFile(file)) {
        assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getMethod())
                .isEqualTo(ZipEntry.STORED);
        assertThat(jarFile.getEntry("BOOT-INF/classes/test/nested.jar").getMethod()).isEqualTo(ZipEntry.STORED);
    }//from   w ww.  jav a 2s  . c om
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void existingSourceEntriesTakePrecedenceOverStandardLibraries() throws Exception {
    TestJarFile nested = new TestJarFile(this.temporaryFolder);
    nested.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File nestedFile = nested.getFile();
    this.testJarFile.addFile("BOOT-INF/lib/" + nestedFile.getName(), nested.getFile());
    this.testJarFile.addClass("A.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    long sourceLength = nestedFile.length();
    repackager.repackage((callback) -> {
        nestedFile.delete();/* w  w w  .ja  v  a2 s. co  m*/
        File toZip = RepackagerTests.this.temporaryFolder.newFile();
        ZipUtil.packEntry(toZip, nestedFile);
        callback.library(new Library(nestedFile, LibraryScope.COMPILE));
    });
    try (JarFile jarFile = new JarFile(file)) {
        assertThat(jarFile.getEntry("BOOT-INF/lib/" + nestedFile.getName()).getSize()).isEqualTo(sourceLength);
    }
}

From source file:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void layoutCanOmitLibraries() throws IOException {
    TestJarFile libJar = new TestJarFile(this.temporaryFolder);
    libJar.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File libJarFile = libJar.getFile();
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    Layout layout = mock(Layout.class);
    LibraryScope scope = mock(LibraryScope.class);
    repackager.setLayout(layout);//from w w  w .  ja v a  2  s . co  m
    repackager.repackage((callback) -> callback.library(new Library(libJarFile, scope)));
    assertThat(getEntryNames(file)).containsExactly("META-INF/", "META-INF/MANIFEST.MF", "a/", "a/b/",
            "a/b/C.class");
}