Skip to content

Commit

Permalink
Merge pull request #47 from hanakeichen/master
Browse files Browse the repository at this point in the history
fix "Error: Resource pattern specifies unknown module file" build error on Gradle application
  • Loading branch information
hazendaz committed May 12, 2023
2 parents 49df9d4 + bfefd9f commit caeacba
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.reflect.Modifier;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Optional;
Expand Down Expand Up @@ -281,13 +282,29 @@ private String toPath(Resource resource, String baseUrl) {
path = url.replace(baseUrl, "");
} else if (url.contains(".jar!")) {
path = JAR_RESOURCE_PREFIX_PATTERN.matcher(url).replaceFirst("");
} else {
path = determineRelativePath(resource);
}
return path;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private String determineRelativePath(Resource resource) throws IOException {
Path path = resource.getFile().toPath();
StringBuilder sb = new StringBuilder();
for (int i = path.getNameCount() - 1; i >= 0; i--) {
sb.insert(0, path.getName(i));
String relativePath = sb.toString();
if (RESOURCE_PATTERN_RESOLVER.getResource(relativePath).exists()) {
return relativePath;
}
sb.insert(0, '/');
}
return resource.getURL().toString();
}

}

static class RepeatableRegistrar extends Registrar {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2022 the original author or authors.
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.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="gradle.OutputMapper">

<select id="select">
SELECT 1
</select>

</mapper>

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
*/
package org.mybatis.spring.nativex;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -33,6 +38,7 @@
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.nativex.hint.TypeAccess;
import org.springframework.util.ClassUtils;

class MyBatisResourcesScanTest {

Expand Down Expand Up @@ -180,6 +186,26 @@ void scanResourceLocations() {
Assertions.assertThat(holder.getReflectionTypeAccesses()).isEmpty();
}

@Test
void scanResourceLocationsWithGradleBuildOutput() throws Exception {
String currentDir = Objects.requireNonNull(getClass().getResource("")).getPath();
String projectDir = currentDir.substring(0, currentDir.indexOf("/target/test-classes"));
SimulateGradleBuildResourcesURLClassLoader classLoader = new SimulateGradleBuildResourcesURLClassLoader(
projectDir + "/src/test/gradle_resources/", getClass().getClassLoader());
ClassUtils.overrideThreadContextClassLoader(classLoader);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.registerBean(ConfigurationForScanResourceLocationsWithGradleOutputResources.class);
context.refresh();
MyBatisScannedResourcesHolder holder = context.getBean(MyBatisScannedResourcesHolder.class);
Assertions.assertThat(holder.getTypeAliasesClasses()).isEmpty();
Assertions.assertThat(holder.getMapperLocations()).isEmpty();
Assertions.assertThat(holder.getTypeHandlerClasses()).isEmpty();
Assertions.assertThat(holder.getReflectionClasses()).isEmpty();
Assertions.assertThat(holder.getResourceLocations())
.containsExactlyInAnyOrder("mapper/gradle_output/GradleOutputMapper.xml");
Assertions.assertThat(holder.getReflectionTypeAccesses()).isEmpty();
}

@Test
void scanResourceLocationsWithMultiPattern() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Expand Down Expand Up @@ -277,6 +303,11 @@ static class ConfigurationForScanReflectionTypeWithMultiPackage {
static class ConfigurationForScanResourceLocations {
}

@MyBatisResourcesScan(resourceLocationPatterns = { "mapper/gradle_output/*.*" })
@Configuration
static class ConfigurationForScanResourceLocationsWithGradleOutputResources {
}

@MyBatisResourcesScan(resourceLocationPatterns = { "mapper/sub2/*.*", "org/apache/ibatis/builder/xml/*.dtd" })
@Configuration
static class ConfigurationForScanResourceLocationsWithMultiPattern {
Expand All @@ -292,4 +323,18 @@ static class ConfigurationForRepeat {

}

private static class SimulateGradleBuildResourcesURLClassLoader extends URLClassLoader {

public SimulateGradleBuildResourcesURLClassLoader(String path, ClassLoader classLoader)
throws MalformedURLException {
super(new URL[] { new File(path).toURI().toURL() }, classLoader);
}

@Override
public void addURL(URL url) {
super.addURL(url);
}

}

}

0 comments on commit caeacba

Please sign in to comment.