3131 * Build an image from Dockerfile.
3232 *
3333 */
34- public class BuildImageCmdImpl extends AbstrDockerCmd <BuildImageCmd , InputStream > implements BuildImageCmd {
34+ public class BuildImageCmdImpl extends
35+ AbstrDockerCmd <BuildImageCmd , InputStream > implements BuildImageCmd {
3536
3637 private static final Pattern ADD_OR_COPY_PATTERN = Pattern
3738 .compile ("^(ADD|COPY)\\ s+(.*)\\ s+(.*)$" );
@@ -62,8 +63,8 @@ public BuildImageCmdImpl(BuildImageCmd.Exec exec, InputStream tarInputStream) {
6263 super (exec );
6364 Preconditions .checkNotNull (tarInputStream , "tarInputStream is null" );
6465 withTarInputStream (tarInputStream );
65- }
66-
66+ }
67+
6768 @ Override
6869 public InputStream getTarInputStream () {
6970 return tarInputStream ;
@@ -75,7 +76,7 @@ public BuildImageCmdImpl withTarInputStream(InputStream tarInputStream) {
7576 this .tarInputStream = tarInputStream ;
7677 return this ;
7778 }
78-
79+
7980 @ Override
8081 public BuildImageCmdImpl withTag (String tag ) {
8182 Preconditions .checkNotNull (tag , "Tag is null" );
@@ -113,7 +114,7 @@ public BuildImageCmdImpl withNoCache(boolean noCache) {
113114 this .noCache = noCache ;
114115 return this ;
115116 }
116-
117+
117118 @ Override
118119 public BuildImageCmdImpl withRemove () {
119120 return withRemove (true );
@@ -124,7 +125,7 @@ public BuildImageCmdImpl withRemove(boolean rm) {
124125 this .remove = rm ;
125126 return this ;
126127 }
127-
128+
128129 @ Override
129130 public BuildImageCmdImpl withQuiet () {
130131 return withQuiet (true );
@@ -142,7 +143,7 @@ public void close() throws IOException {
142143 if (tarFile != null ) {
143144 FileUtils .deleteQuietly (tarFile );
144145 }
145-
146+
146147 tarInputStream .close ();
147148 }
148149
@@ -152,8 +153,7 @@ public String toString() {
152153 .append (tag != null ? "-t " + tag + " " : "" )
153154 .append (noCache ? "--nocache=true " : "" )
154155 .append (quiet ? "--quiet=true " : "" )
155- .append (!remove ? "--rm=false " : "" )
156- .toString ();
156+ .append (!remove ? "--rm=false " : "" ).toString ();
157157 }
158158
159159 protected File buildDockerFolderTar (File dockerFolder ) {
@@ -182,23 +182,30 @@ protected File buildDockerFolderTar(File dockerFolder) {
182182 File dockerIgnoreFile = new File (dockerFolder , ".dockerignore" );
183183 if (dockerIgnoreFile .exists ()) {
184184 int lineNumber = 0 ;
185- List <String > dockerIgnoreFileContent = FileUtils .readLines (dockerIgnoreFile );
186- for (String pattern : dockerIgnoreFileContent ) {
185+ List <String > dockerIgnoreFileContent = FileUtils
186+ .readLines (dockerIgnoreFile );
187+ for (String pattern : dockerIgnoreFileContent ) {
187188 lineNumber ++;
188189 pattern = pattern .trim ();
189190 if (pattern .isEmpty ()) {
190191 continue ; // skip empty lines
191192 }
192193 pattern = FilenameUtils .normalize (pattern );
193194 try {
194- // validate pattern and make sure we aren't excluding Dockerfile
195+ // validate pattern and make sure we aren't excluding
196+ // Dockerfile
195197 if (GoLangFileMatch .match (pattern , "Dockerfile" )) {
196198 throw new DockerClientException (
197- String .format ("Dockerfile is excluded by pattern '%s' on line %s in .dockerignore file" , pattern , lineNumber ));
199+ String .format (
200+ "Dockerfile is excluded by pattern '%s' on line %s in .dockerignore file" ,
201+ pattern , lineNumber ));
198202 }
199203 ignores .add (pattern );
200204 } catch (GoLangFileMatchException e ) {
201- throw new DockerClientException (String .format ("Invalid pattern '%s' on line %s in .dockerignore file" , pattern , lineNumber ));
205+ throw new DockerClientException (
206+ String .format (
207+ "Invalid pattern '%s' on line %s in .dockerignore file" ,
208+ pattern , lineNumber ));
202209 }
203210 }
204211 }
@@ -254,19 +261,26 @@ protected File buildDockerFolderTar(File dockerFolder) {
254261 src , dockerFolder ));
255262 }
256263
257- if (!src .exists ()) {
258- throw new DockerClientException (String .format (
259- "Source file %s doesn't exist" , src ));
260- }
264+ // if (!src.exists()) {
265+ // throw new DockerClientException(String.format(
266+ // "Source file %s doesn't exist", src));
267+ // }
261268 if (src .isDirectory ()) {
262269 Collection <File > files = FileUtils .listFiles (src ,
263- new GoLangMatchFileFilter (src , ignores ), TrueFileFilter .INSTANCE );
270+ new GoLangMatchFileFilter (src , ignores ),
271+ TrueFileFilter .INSTANCE );
264272 filesToAdd .addAll (files );
265- } else if (!GoLangFileMatch .match (ignores , CompressArchiveUtil .relativize (dockerFolder , src ))){
273+ } else if (!src .exists ()) {
274+ filesToAdd .addAll (resolveWildcards (src , ignores ));
275+ } else if (!GoLangFileMatch .match (ignores ,
276+ CompressArchiveUtil .relativize (dockerFolder ,
277+ src ))) {
266278 filesToAdd .add (src );
267279 } else {
268- throw new DockerClientException (String .format (
269- "Source file %s is excluded by .dockerignore file" , src ));
280+ throw new DockerClientException (
281+ String .format (
282+ "Source file %s is excluded by .dockerignore file" ,
283+ src ));
270284 }
271285 }
272286 }
@@ -281,6 +295,27 @@ protected File buildDockerFolderTar(File dockerFolder) {
281295 }
282296 }
283297
298+ private Collection <File > resolveWildcards (File file , List <String > ignores ) {
299+ List <File > filesToAdd = new ArrayList <File >();
300+
301+ File parent = file .getParentFile ();
302+ if (parent != null ) {
303+ if (parent .isDirectory ()) {
304+ Collection <File > files = FileUtils .listFiles (parent ,
305+ new GoLangMatchFileFilter (parent , ignores ),
306+ TrueFileFilter .INSTANCE );
307+ filesToAdd .addAll (files );
308+ } else {
309+ filesToAdd .addAll (resolveWildcards (parent , ignores ));
310+ }
311+ } else {
312+ throw new DockerClientException (String .format (
313+ "Source file %s doesn't exist" , file ));
314+ }
315+
316+ return filesToAdd ;
317+ }
318+
284319 private String filterForEnvironmentVars (String extractedResource ,
285320 Map <String , String > environmentMap ) {
286321
0 commit comments