|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.context.embedded; |
| 18 | + |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileOutputStream; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Enumeration; |
| 26 | +import java.util.List; |
| 27 | +import java.util.jar.JarEntry; |
| 28 | +import java.util.jar.JarFile; |
| 29 | + |
| 30 | +import org.springframework.util.FileSystemUtils; |
| 31 | +import org.springframework.util.StreamUtils; |
| 32 | +import org.springframework.util.StringUtils; |
| 33 | + |
| 34 | +/** |
| 35 | + * {@link AbstractApplicationLauncher} that launches a Spring Boot application with a |
| 36 | + * classpath similar to that used when run with Maven or Gradle. |
| 37 | + * |
| 38 | + * @author Andy Wilkinson |
| 39 | + */ |
| 40 | +class BootRunApplicationLauncher extends AbstractApplicationLauncher { |
| 41 | + |
| 42 | + private final File exploded = new File("target/run"); |
| 43 | + |
| 44 | + BootRunApplicationLauncher(ApplicationBuilder applicationBuilder) { |
| 45 | + super(applicationBuilder); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected List<String> getArguments(File archive) { |
| 50 | + try { |
| 51 | + explodeArchive(archive); |
| 52 | + deleteLauncherClasses(); |
| 53 | + File targetClasses = populateTargetClasses(archive); |
| 54 | + File dependencies = populateDependencies(archive); |
| 55 | + populateSrcMainWebapp(); |
| 56 | + List<String> classpath = new ArrayList<String>(); |
| 57 | + classpath.add(targetClasses.getAbsolutePath()); |
| 58 | + for (File dependency : dependencies.listFiles()) { |
| 59 | + classpath.add(dependency.getAbsolutePath()); |
| 60 | + } |
| 61 | + return Arrays.asList("-cp", |
| 62 | + StringUtils.collectionToDelimitedString(classpath, |
| 63 | + File.pathSeparator), |
| 64 | + "com.example.ResourceHandlingApplication"); |
| 65 | + } |
| 66 | + catch (IOException ex) { |
| 67 | + throw new RuntimeException(ex); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private void deleteLauncherClasses() { |
| 72 | + FileSystemUtils.deleteRecursively(new File(this.exploded, "org")); |
| 73 | + } |
| 74 | + |
| 75 | + private File populateTargetClasses(File archive) { |
| 76 | + File targetClasses = new File(this.exploded, "target/classes"); |
| 77 | + targetClasses.mkdirs(); |
| 78 | + new File(this.exploded, getClassesPath(archive)).renameTo(targetClasses); |
| 79 | + return targetClasses; |
| 80 | + } |
| 81 | + |
| 82 | + private File populateDependencies(File archive) { |
| 83 | + File dependencies = new File(this.exploded, "dependencies"); |
| 84 | + dependencies.mkdirs(); |
| 85 | + List<String> libPaths = getLibPaths(archive); |
| 86 | + for (String libPath : libPaths) { |
| 87 | + for (File jar : new File(this.exploded, libPath).listFiles()) { |
| 88 | + jar.renameTo(new File(dependencies, jar.getName())); |
| 89 | + } |
| 90 | + } |
| 91 | + return dependencies; |
| 92 | + } |
| 93 | + |
| 94 | + private void populateSrcMainWebapp() { |
| 95 | + File srcMainWebapp = new File(this.exploded, "src/main/webapp"); |
| 96 | + srcMainWebapp.mkdirs(); |
| 97 | + new File(this.exploded, "webapp-resource.txt") |
| 98 | + .renameTo(new File(srcMainWebapp, "webapp-resource.txt")); |
| 99 | + } |
| 100 | + |
| 101 | + private String getClassesPath(File archive) { |
| 102 | + return archive.getName().endsWith(".jar") ? "BOOT-INF/classes" |
| 103 | + : "WEB-INF/classes"; |
| 104 | + } |
| 105 | + |
| 106 | + private List<String> getLibPaths(File archive) { |
| 107 | + return archive.getName().endsWith(".jar") |
| 108 | + ? Collections.singletonList("BOOT-INF/lib") |
| 109 | + : Arrays.asList("WEB-INF/lib", "WEB-INF/lib-provided"); |
| 110 | + } |
| 111 | + |
| 112 | + private void explodeArchive(File archive) throws IOException { |
| 113 | + FileSystemUtils.deleteRecursively(this.exploded); |
| 114 | + JarFile jarFile = new JarFile(archive); |
| 115 | + Enumeration<JarEntry> entries = jarFile.entries(); |
| 116 | + while (entries.hasMoreElements()) { |
| 117 | + JarEntry jarEntry = entries.nextElement(); |
| 118 | + File extracted = new File(this.exploded, jarEntry.getName()); |
| 119 | + if (jarEntry.isDirectory()) { |
| 120 | + extracted.mkdirs(); |
| 121 | + } |
| 122 | + else { |
| 123 | + FileOutputStream extractedOutputStream = new FileOutputStream(extracted); |
| 124 | + StreamUtils.copy(jarFile.getInputStream(jarEntry), extractedOutputStream); |
| 125 | + extractedOutputStream.close(); |
| 126 | + } |
| 127 | + } |
| 128 | + jarFile.close(); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected File getWorkingDirectory() { |
| 133 | + return this.exploded; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected String getDescription(String packaging) { |
| 138 | + return "build system run " + packaging + " project"; |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments