-
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.
Adds jib:dockercontext to export a Docker context. (#121)
- Loading branch information
Showing
15 changed files
with
704 additions
and
112 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
49 changes: 49 additions & 0 deletions
49
jib-core/src/main/java/com/google/cloud/tools/jib/builder/EntrypointBuilder.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,49 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* 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.builder; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** Builds an image entrypoint for the Java application. */ | ||
public class EntrypointBuilder { | ||
|
||
/** | ||
* Builds the container entrypoint. | ||
* | ||
* <p>The entrypoint is {@code java [jvm flags] -cp [classpaths] [main class]}. | ||
*/ | ||
public static List<String> makeEntrypoint( | ||
SourceFilesConfiguration sourceFilesConfiguration, List<String> jvmFlags, String mainClass) { | ||
List<String> classPaths = new ArrayList<>(); | ||
classPaths.add(sourceFilesConfiguration.getDependenciesPathOnImage() + "*"); | ||
classPaths.add(sourceFilesConfiguration.getResourcesPathOnImage()); | ||
classPaths.add(sourceFilesConfiguration.getClassesPathOnImage()); | ||
|
||
String classPathsString = String.join(":", classPaths); | ||
|
||
List<String> entrypoint = new ArrayList<>(4 + jvmFlags.size()); | ||
entrypoint.add("java"); | ||
entrypoint.addAll(jvmFlags); | ||
entrypoint.add("-cp"); | ||
entrypoint.add(classPathsString); | ||
entrypoint.add(mainClass); | ||
return entrypoint; | ||
} | ||
|
||
private EntrypointBuilder() {} | ||
} |
58 changes: 58 additions & 0 deletions
58
jib-core/src/main/java/com/google/cloud/tools/jib/filesystem/DirectoryWalker.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,58 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* 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.filesystem; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.stream.Stream; | ||
|
||
/** Recursively applies a function to each file in a directory. */ | ||
public class DirectoryWalker { | ||
|
||
private final Path rootDir; | ||
|
||
/** Initialize with a root directory to walk. */ | ||
public DirectoryWalker(Path rootDir) { | ||
if (!Files.isDirectory(rootDir)) { | ||
throw new IllegalArgumentException("rootDir must be a directory"); | ||
} | ||
this.rootDir = rootDir; | ||
} | ||
|
||
/** | ||
* Walks {@link #rootDir} and applies {@code pathConsumer} to each file. Note that {@link | ||
* #rootDir} itself is visited as well. | ||
*/ | ||
public void walk(PathConsumer pathConsumer) throws IOException { | ||
try (Stream<Path> fileStream = Files.walk(rootDir)) { | ||
fileStream.forEach( | ||
path -> { | ||
try { | ||
pathConsumer.accept(path); | ||
|
||
} catch (IOException ex) { | ||
throw new UncheckedIOException(ex); | ||
} | ||
}); | ||
|
||
} catch (UncheckedIOException ex) { | ||
throw ex.getCause(); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
jib-core/src/main/java/com/google/cloud/tools/jib/filesystem/PathConsumer.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,26 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* 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.filesystem; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
|
||
@FunctionalInterface | ||
public interface PathConsumer { | ||
|
||
void accept(Path path) throws IOException; | ||
} |
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
54 changes: 54 additions & 0 deletions
54
jib-core/src/test/java/com/google/cloud/tools/jib/filesystem/DirectoryWalkerTest.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,54 @@ | ||
/* | ||
* Copyright 2018 Google Inc. | ||
* | ||
* 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.filesystem; | ||
|
||
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 java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** Tests for {@link DirectoryWalker}. */ | ||
public class DirectoryWalkerTest { | ||
|
||
@Test | ||
public void testWalk() throws URISyntaxException, IOException { | ||
Path testDir = Paths.get(Resources.getResource("layer").toURI()); | ||
|
||
Set<Path> walkedPaths = new HashSet<>(); | ||
PathConsumer addToWalkedPaths = walkedPaths::add; | ||
|
||
new DirectoryWalker(testDir).walk(addToWalkedPaths); | ||
|
||
Set<Path> expectedPaths = | ||
new HashSet<>( | ||
Arrays.asList( | ||
testDir, | ||
testDir.resolve("a"), | ||
testDir.resolve("a").resolve("b"), | ||
testDir.resolve("a").resolve("b").resolve("bar"), | ||
testDir.resolve("c"), | ||
testDir.resolve("c").resolve("cat"), | ||
testDir.resolve("foo"))); | ||
Assert.assertEquals(expectedPaths, walkedPaths); | ||
} | ||
} |
Oops, something went wrong.