Example usage for org.springframework.boot.loader.tools Repackager repackage

List of usage examples for org.springframework.boot.loader.tools Repackager repackage

Introduction

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

Prototype

public void repackage(Libraries libraries) throws IOException 

Source Link

Document

Repackage the source file so that it can be run using ' java -jar '.

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 {//from  w w  w. jav  a 2s.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:org.springframework.boot.loader.tools.RepackagerTests.java

@Test
public void specificMainClass() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.setMainClass("a.b.C");
    repackager.repackage(NO_LIBRARIES);
    Manifest actualManifest = getManifest(file);
    assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
            .isEqualTo("org.springframework.boot.loader.JarLauncher");
    assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
    assertThat(hasLauncherClasses(file)).isTrue();
}

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

@Test
public void mainClassFromManifest() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithoutMainMethod.class);
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue("Main-Class", "a.b.C");
    this.testJarFile.addManifest(manifest);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(NO_LIBRARIES);
    Manifest actualManifest = getManifest(file);
    assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
            .isEqualTo("org.springframework.boot.loader.JarLauncher");
    assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
    assertThat(hasLauncherClasses(file)).isTrue();
}

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

@Test
public void mainClassFound() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(NO_LIBRARIES);
    Manifest actualManifest = getManifest(file);
    assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
            .isEqualTo("org.springframework.boot.loader.JarLauncher");
    assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
    assertThat(hasLauncherClasses(file)).isTrue();
}

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

@Test
public void jarIsOnlyRepackagedOnce() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(NO_LIBRARIES);
    repackager.repackage(NO_LIBRARIES);/*  w  ww  . j a v a  2s.  c o m*/
    Manifest actualManifest = getManifest(file);
    assertThat(actualManifest.getMainAttributes().getValue("Main-Class"))
            .isEqualTo("org.springframework.boot.loader.JarLauncher");
    assertThat(actualManifest.getMainAttributes().getValue("Start-Class")).isEqualTo("a.b.C");
    assertThat(hasLauncherClasses(file)).isTrue();
}

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

@Test
public void multipleMainClassFound() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    this.testJarFile.addClass("a/b/D.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage(
            "Unable to find a single main class " + "from the following candidates [a.b.C, a.b.D]");
    repackager.repackage(NO_LIBRARIES);
}

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

@Test
public void sameSourceAndDestinationWithBackup() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.repackage(NO_LIBRARIES);
    assertThat(new File(file.getParent(), file.getName() + ".original")).exists();
    assertThat(hasLauncherClasses(file)).isTrue();
}

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

@Test
public void sameSourceAndDestinationWithoutBackup() throws Exception {
    this.testJarFile.addClass("a/b/C.class", ClassWithMainMethod.class);
    File file = this.testJarFile.getFile();
    Repackager repackager = new Repackager(file);
    repackager.setBackupSource(false);//www  .j  a v  a 2s .  c o m
    repackager.repackage(NO_LIBRARIES);
    assertThat(new File(file.getParent(), file.getName() + ".original")).doesNotExist();
    assertThat(hasLauncherClasses(file)).isTrue();
}

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  . j  ava2 s.com
    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 duplicateLibraries() 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);
    this.thrown.expect(IllegalStateException.class);
    this.thrown.expectMessage("Duplicate library");
    repackager.repackage((callback) -> {
        callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
        callback.library(new Library(libJarFile, LibraryScope.COMPILE, false));
    });//from  w w  w  .j  ava2 s .com
}