-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement JarProcessor to determine jar type. (#2797)
* Implement JarProcessor to determine jar type and use Truth for tests
- Loading branch information
Showing
6 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
jib-cli/src/main/java/com/google/cloud/tools/jib/cli/jar/JarProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2020 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.cli.jar; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.jar.JarFile; | ||
|
||
public class JarProcessor { | ||
|
||
/** | ||
* Jar Type. | ||
* | ||
* <ul> | ||
* <li>{@code STANDARD} a regular jar. | ||
* <li>{@code SPRING_BOOT} a spring boot fat jar. | ||
* </ul> | ||
*/ | ||
public enum JarType { | ||
STANDARD, | ||
SPRING_BOOT; | ||
} | ||
|
||
/** | ||
* Determines whether the jar is a spring boot or regular jar, given a path to the jar. | ||
* | ||
* @param jarPath path to the jar. | ||
* @return the jar type. | ||
* @throws IOException if I/O error occurs when opening the file. | ||
*/ | ||
public static JarType determineJarType(Path jarPath) throws IOException { | ||
JarFile jarFile = new JarFile(jarPath.toFile()); | ||
if (jarFile.getEntry("BOOT-INF") != null) { | ||
return JarType.SPRING_BOOT; | ||
} | ||
return JarType.STANDARD; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
jib-cli/src/test/java/com/google/cloud/tools/jib/cli/jar/JarProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2020 Google LLC. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.cloud.tools.jib.cli.jar; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.cloud.tools.jib.cli.jar.JarProcessor.JarType; | ||
import com.google.common.io.Resources; | ||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import org.junit.Test; | ||
|
||
public class JarProcessorTest { | ||
|
||
private static final String SPRING_BOOT_RESOURCE_DIR = "jar/springboot/springboot_sample.jar"; | ||
private static final String STANDARD_RESOURCE_DIR = "jar/standard/standardJar.jar"; | ||
|
||
@Test | ||
public void testDetermineJarType_springBoot() throws IOException, URISyntaxException { | ||
Path springBootJar = Paths.get(Resources.getResource(SPRING_BOOT_RESOURCE_DIR).toURI()); | ||
JarType jarType = JarProcessor.determineJarType(springBootJar); | ||
assertThat(jarType).isEqualTo(JarType.SPRING_BOOT); | ||
} | ||
|
||
@Test | ||
public void testDetermineJarType_standard() throws IOException, URISyntaxException { | ||
Path standardJar = Paths.get(Resources.getResource(STANDARD_RESOURCE_DIR).toURI()); | ||
JarType jarType = JarProcessor.determineJarType(standardJar); | ||
assertThat(jarType).isEqualTo(JarType.STANDARD); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.