DumpClassesTask.java revision 3062:15bdc18525ff
1/*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package anttasks;
27
28import java.io.File;
29import java.io.IOException;
30import java.io.UncheckedIOException;
31import java.net.URI;
32import java.net.URISyntaxException;
33import java.nio.file.FileSystem;
34import java.nio.file.FileSystems;
35import java.nio.file.Files;
36import java.nio.file.Path;
37import java.nio.file.StandardCopyOption;
38import java.util.Collections;
39import java.util.stream.Stream;
40
41import org.apache.tools.ant.BuildException;
42import org.apache.tools.ant.Task;
43
44public class DumpClassesTask extends Task {
45
46    private String moduleName;
47    private File dir;
48
49    public void setModuleName(String moduleName) {
50        this.moduleName = moduleName;
51    }
52
53    public void setDestDir(File dir) {
54        this.dir = dir;
55    }
56
57    @Override
58    public void execute() {
59        try (FileSystem fs = FileSystems.newFileSystem(new URI("jrt:/"), Collections.emptyMap(), DumpClassesTask.class.getClassLoader())) {
60            Path source = fs.getPath("modules", moduleName);
61            Path target = dir.toPath();
62
63            try (Stream<Path> content = Files.walk(source)) {
64                content.filter(Files :: isRegularFile)
65                       .forEach(p -> {
66                    try {
67                        Path targetFile = target.resolve(source.relativize(p).toString());
68                        if (!Files.exists(targetFile) || Files.getLastModifiedTime(targetFile).compareTo(Files.getLastModifiedTime(source)) < 0) {
69                            Files.createDirectories(targetFile.getParent());
70                            Files.copy(p, targetFile, StandardCopyOption.REPLACE_EXISTING);
71                        }
72                    } catch (IOException ex) {
73                        throw new UncheckedIOException(ex);
74                    }
75                });
76            }
77        } catch (URISyntaxException | IOException | UncheckedIOException ex) {
78            throw new BuildException(ex);
79        }
80    }
81}
82