Skip to content

Instantly share code, notes, and snippets.

@irof
Created Aug 15, 2012
Embed
What would you like to do?
GroovyでJavaのpackage-info.javaを一括生成するスクリプト
RubyでJavaのpackage-info.javaを一括生成するスクリプト
http://d.hatena.ne.jp/tbpg/20120808/1344447924
をGroovyにしてみた。
import java.nio.file.*
import java.nio.file.attribute.*
import static java.nio.file.FileVisitResult.CONTINUE
import static groovy.io.FileType.FILES
def PACKAGE_INFO = 'package-info.java'
def SOURCE_DIR = './src'
def package_detail = '// TODO パッケージ内容の詳細を記述してください'
def generate_package_info_code = { package_name ->
"""\
/**
* ${package_name}パッケージ。
*
* <pre>
* ${package_detail}
* </pre>
*
*/
package ${package_name};
"""
}
def generate_package_info_file = {file, code ->
Files.write(file.resolve(PACKAGE_INFO), code.getBytes())
}
Path root = Paths.get(SOURCE_DIR)
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
public FileVisitResult preVisitDirectory(Path file, BasicFileAttributes attrs) {
if (root == file) return CONTINUE
def name = root.relativize(file).toString().replaceAll('/', '.')
def package_info_code = generate_package_info_code(name)
generate_package_info_file(file, package_info_code)
CONTINUE
}
}
);
println "出力完了。出力結果"
root.toFile().eachFileRecurse(FILES) {
if (it.name == PACKAGE_INFO) println it
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment