diff --git a/modules/openapi-generator-maven-plugin/pom.xml b/modules/openapi-generator-maven-plugin/pom.xml
index 9252181f3f84..fd6659a2dc0a 100644
--- a/modules/openapi-generator-maven-plugin/pom.xml
+++ b/modules/openapi-generator-maven-plugin/pom.xml
@@ -62,6 +62,12 @@
test
+
+ org.apache.maven
+ maven-resolver-provider
+ 3.6.3
+ test
+
org.apache.maven.shared
maven-verifier
diff --git a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java
index 8949156f4d8e..5cab118dbb9b 100644
--- a/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java
+++ b/modules/openapi-generator-maven-plugin/src/main/java/org/openapitools/codegen/plugin/CodeGenMojo.java
@@ -28,11 +28,13 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
+import java.net.URLClassLoader;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -42,13 +44,15 @@
import com.google.common.io.CharSource;
import io.swagger.v3.parser.util.ClasspathHelper;
import org.apache.commons.lang3.StringUtils;
+import org.apache.maven.artifact.Artifact;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
-import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.openapitools.codegen.CliOption;
@@ -71,7 +75,7 @@
* Goal which generates client/server code from a OpenAPI json/yaml definition.
*/
@SuppressWarnings({"unused", "MismatchedQueryAndUpdateOfCollection"})
-@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, threadSafe = true)
+@Mojo(name = "generate", defaultPhase = LifecyclePhase.GENERATE_SOURCES, requiresDependencyResolution = ResolutionScope.COMPILE, threadSafe = true)
public class CodeGenMojo extends AbstractMojo {
private final Logger LOGGER = LoggerFactory.getLogger(CodeGenMojo.class);
@@ -500,7 +504,13 @@ public void execute() throws MojoExecutionException {
}
if (isNotEmpty(inputSpec)) {
- configurator.setInputSpec(inputSpec);
+ URL url = inputSpecRemoteUrl();
+
+ if ((! inputSpecFile.exists()) && url != null) {
+ configurator.setInputSpec(url.toString());
+ } else {
+ configurator.setInputSpec(inputSpec);
+ }
}
if (isNotEmpty(gitHost)) {
@@ -825,15 +835,35 @@ private String calculateInputSpecHash(File inputSpecFile) throws IOException {
}
/**
- * Try to parse inputSpec setting string into URL
+ * Try to parse inputSpec setting string into URL (truly remote or resource)
* @return A valid URL or null if inputSpec is not a valid URL
*/
- private URL inputSpecRemoteUrl(){
- try {
- return new URI(inputSpec).toURL();
- } catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
- return null;
+ private URL inputSpecRemoteUrl() {
+ URL url = dependencyClassLoader().getResource(inputSpec);
+
+ if (url == null) {
+ try {
+ url = new URI(inputSpec).toURL();
+ } catch (URISyntaxException | MalformedURLException | IllegalArgumentException e) {
+ }
}
+
+ return url;
+ }
+
+ private ClassLoader dependencyClassLoader() {
+ List list = new ArrayList<>();
+
+ for (Artifact artifact : project.getArtifacts()) {
+ try {
+ if (artifact.isResolved() && artifact.getType().equals("jar")) {
+ list.add(new URL("jar:" + artifact.getFile().toURI() + "!/"));
+ }
+ } catch (Exception e) {
+ }
+ }
+
+ return new URLClassLoader(list.toArray(new URL[] { }), getClass().getClassLoader());
}
/**
diff --git a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java
index ad69be7e7209..87b7a72a613a 100644
--- a/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java
+++ b/modules/openapi-generator-maven-plugin/src/test/java/org/openapitools/codegen/plugin/CodeGenMojoTest.java
@@ -24,7 +24,11 @@
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingRequest;
+import org.apache.maven.repository.internal.MavenRepositorySystemUtils;
import org.eclipse.aether.DefaultRepositorySystemSession;
+import org.eclipse.aether.RepositorySystem;
+import org.eclipse.aether.impl.DefaultServiceLocator;
+import org.eclipse.aether.repository.LocalRepository;
import org.openapitools.codegen.plugin.stubs.StubUtility;
import java.io.File;
@@ -38,10 +42,22 @@ protected void setUp() throws Exception {
super.setUp();
}
+ public void testCommonConfigurationWithFileInputSpec() throws Exception {
+ testCommonConfiguration("file");
+ }
+
+ public void testCommonConfigurationWithResourceInputSpec() throws Exception {
+ testCommonConfiguration("resource");
+ }
+
+ public void testCommonConfigurationWithURLInputSpec() throws Exception {
+ testCommonConfiguration("url");
+ }
+
@SuppressWarnings("unchecked")
- public void testCommonConfiguration() throws Exception {
+ private void testCommonConfiguration(String profile) throws Exception {
File folder = Files.createTempDirectory("test").toFile();
- CodeGenMojo mojo = loadMojo(folder, "src/test/resources/default");
+ CodeGenMojo mojo = loadMojo(folder, "src/test/resources/default", profile);
mojo.execute();
assertEquals("java", getVariableValueFromObject(mojo, "generatorName"));
assertEquals("jersey2", getVariableValueFromObject(mojo, "library"));
@@ -57,7 +73,7 @@ public void testCommonConfiguration() throws Exception {
public void testHashGenerationFileContainsExecutionId() throws Exception {
// GIVEN
Path folder = Files.createTempDirectory("test");
- CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "executionId");
+ CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/default", "file", "executionId");
// WHEN
mojo.execute();
@@ -67,14 +83,14 @@ public void testHashGenerationFileContainsExecutionId() throws Exception {
assertTrue(hashFolder.resolve("petstore.yaml-executionId.sha256").toFile().exists());
}
- protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot) throws Exception {
- return loadMojo(temporaryFolder, projectRoot, "default");
+ protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String profile) throws Exception {
+ return loadMojo(temporaryFolder, projectRoot, profile, "default");
}
- protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String executionId) throws Exception {
+ protected CodeGenMojo loadMojo(File temporaryFolder, String projectRoot, String profile, String executionId) throws Exception {
File file = new File(projectRoot);
FileUtils.copyDirectory(file, temporaryFolder);
- MavenProject project = readMavenProject(temporaryFolder);
+ MavenProject project = readMavenProject(temporaryFolder, profile);
MavenSession session = newMavenSession(project);
MojoExecution execution = newMojoExecution("generate");
MojoExecution executionWithId = copyWithExecutionId(executionId, execution);
@@ -87,15 +103,24 @@ private MojoExecution copyWithExecutionId(String executionId, MojoExecution exec
return executionWithId;
}
- protected MavenProject readMavenProject(File basedir)
+ protected MavenProject readMavenProject(File basedir, String profile)
throws Exception {
File pom = new File(basedir, "pom.xml");
+ LocalRepository localRepo = new LocalRepository(new File(basedir, "local-repo"));
+ DefaultServiceLocator locator = MavenRepositorySystemUtils.newServiceLocator();
+ RepositorySystem system = locator.getService(RepositorySystem.class);
+ DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
+ session.setLocalRepositoryManager(system.newLocalRepositoryManager(session, localRepo));
MavenExecutionRequest request = new DefaultMavenExecutionRequest();
request.setBaseDirectory(basedir);
+ if (profile != null) {
+ request.addActiveProfile(profile);
+ }
ProjectBuildingRequest configuration = request.getProjectBuildingRequest();
- configuration.setRepositorySession(new DefaultRepositorySystemSession());
+ configuration.setRepositorySession(session);
+ configuration.setResolveDependencies(true);
MavenProject project = lookup(ProjectBuilder.class).build(pom, configuration).getProject();
assertNotNull(project);
return project;
}
-}
\ No newline at end of file
+}
diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/1/schema-1.jar b/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/1/schema-1.jar
new file mode 100644
index 000000000000..5b4d1c0860f7
Binary files /dev/null and b/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/1/schema-1.jar differ
diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/1/schema-1.pom b/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/1/schema-1.pom
new file mode 100644
index 000000000000..bd1c51309d9a
--- /dev/null
+++ b/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/1/schema-1.pom
@@ -0,0 +1,9 @@
+
+
+ 4.0.0
+ petstore
+ schema
+ 1
+ POM was created from install:install-file
+
diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/maven-metadata-local.xml b/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/maven-metadata-local.xml
new file mode 100644
index 000000000000..d66c798366f3
--- /dev/null
+++ b/modules/openapi-generator-maven-plugin/src/test/resources/default/local-repo/petstore/schema/maven-metadata-local.xml
@@ -0,0 +1,12 @@
+
+
+ petstore
+ schema
+
+ 1
+
+ 1
+
+ 20210724220722
+
+
diff --git a/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml b/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml
index 95ab2451e5d8..dfd850f23099 100644
--- a/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml
+++ b/modules/openapi-generator-maven-plugin/src/test/resources/default/pom.xml
@@ -1,5 +1,5 @@