Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ private AnnotationValue getAnnotationValue(AnnotationMirror annotation, String n

private static final String[] SOURCE_ROOTS = {"src/test/java/", "src/main/java/"};

private static final String[] RESOURCE_ROOTS = {"src/test/resources/", "src/main/resources/"};

private void writeResource() {
int count = collectedPaths.size();
if (count > 0) {
Expand All @@ -228,7 +230,13 @@ private void writeResource() {
Diagnostic.Kind.NOTE,
"Copying " + count + " demo-source " + (count == 1 ? "file" : "files") + " to class output");
}
for (String sourcePath : collectedPaths) {
for (String rawPath : collectedPaths) {
String sourcePath = rawPath.startsWith("/") ? rawPath.substring(1) : rawPath;
// Files under META-INF/resources/ are already packaged into the JAR by Maven's
// resource-processing phase; the processor does not need to copy them.
if (isFromMetaInfResources(sourcePath)) {
continue;
}
try {
FileObject source = openSourceFile(sourcePath);
FileObject resource =
Expand All @@ -248,6 +256,16 @@ private void writeResource() {
}
}

private static boolean isFromMetaInfResources(String normalizedPath) {
for (String root : RESOURCE_ROOTS) {
if (normalizedPath.startsWith(root)) {
String relative = normalizedPath.substring(root.length());
return relative.startsWith("META-INF/resources/") || relative.equals("META-INF/resources");
}
}
return false;
}
Comment thread
paodb marked this conversation as resolved.

private FileObject openSourceFile(String path) throws IOException {
for (String root : SOURCE_ROOTS) {
if (path.startsWith(root)) {
Expand All @@ -256,6 +274,17 @@ private FileObject openSourceFile(String path) throws IOException {
.getResource(StandardLocation.SOURCE_PATH, "", path.substring(root.length()));
}
}
// Resource files (src/test/resources/, src/main/resources/) are copied to CLASS_OUTPUT
// by Maven's process-*-resources phase, which runs before test-compile (and therefore
// before annotation processors). Read them from CLASS_OUTPUT using the path relative to
// the resources root.
for (String root : RESOURCE_ROOTS) {
if (path.startsWith(root)) {
return processingEnv
.getFiler()
.getResource(StandardLocation.CLASS_OUTPUT, "", path.substring(root.length()));
}
}
return processingEnv.getFiler().getResource(StandardLocation.SOURCE_PATH, "", path);
}
}
Loading