diff --git a/modules/openapi-generator-maven-plugin/examples/java-client.xml b/modules/openapi-generator-maven-plugin/examples/java-client.xml
index 1961f1d8f0d7..9036fb77bf34 100644
--- a/modules/openapi-generator-maven-plugin/examples/java-client.xml
+++ b/modules/openapi-generator-maven-plugin/examples/java-client.xml
@@ -70,6 +70,30 @@
true
+
+ jar
+
+ generate
+
+
+
+ jar:file:${project.basedir}/../src/test/resources/default/local-repo/petstore/schema/1/schema-1.jar!/petstore.yaml
+
+
+ java
+
+
+
+
+
+ joda
+
+
+
+ jersey2
+
+
diff --git a/modules/openapi-generator-maven-plugin/pom.xml b/modules/openapi-generator-maven-plugin/pom.xml
index 708c506dff91..2609980037ce 100644
--- a/modules/openapi-generator-maven-plugin/pom.xml
+++ b/modules/openapi-generator-maven-plugin/pom.xml
@@ -16,7 +16,7 @@
UTF-8
**/src/main/java/org/openapitools/codegen/plugin/**/*
-
+ 3.9.6
@@ -27,25 +27,25 @@
org.apache.maven
maven-core
- 3.9.4
+ ${maven.version}
provided
org.apache.maven
maven-artifact
- 3.9.4
+ ${maven.version}
provided
org.apache.maven
maven-compat
- 3.9.4
+ ${maven.version}
test
org.apache.maven
maven-plugin-api
- 3.9.4
+ ${maven.version}
provided
@@ -64,6 +64,12 @@
${junit.version}
test
+
+ org.apache.maven
+ maven-resolver-provider
+ ${maven.version}
+ 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 f7f1af538c84..fc9eab015103 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
@@ -32,6 +32,7 @@
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;
@@ -39,6 +40,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.text.MessageFormat;
+import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
@@ -50,14 +52,17 @@
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.util.ClasspathHelper;
import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.FilenameUtils;
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.apache.maven.project.MavenProjectHelper;
@@ -82,7 +87,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);
@@ -662,7 +667,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)) {
@@ -1075,15 +1086,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(FilenameUtils.separatorsToUnix(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());
}
/**
@@ -1096,7 +1127,7 @@ private File getHashFile(File inputSpecFile) {
String name = inputSpecFile.getName();
URL url = inputSpecRemoteUrl();
- if (url != null) {
+ if (inputSpecFile.exists() && url != null) {
String[] segments = url.getPath().split("/");
name = Files.getNameWithoutExtension(segments[segments.length - 1]);
}
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 89dc94c4300a..d39da0d8a622 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
@@ -37,7 +37,12 @@
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.impl.DefaultServiceLocator;
+import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
+import org.eclipse.aether.repository.LocalRepository;
+import org.eclipse.aether.RepositorySystem;
public class CodeGenMojoTest extends BaseTestCase {
@Override
@@ -45,10 +50,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"));
@@ -65,7 +82,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();
@@ -86,7 +103,7 @@ public void testSkipRegenerationForClasspathSpecFileNoChange() throws Exception
//GIVEN
/* Setup the mojo */
final Path folder = Files.createTempDirectory("test-classpath");
- final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", "executionId");
+ final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", null, "executionId");
/* Perform an initial generation */
mojo.execute();
@@ -124,7 +141,7 @@ public void testSkipRegenerationForClasspathSpecFileWithChange() throws Exceptio
//GIVEN
/* Setup the mojo */
final Path folder = Files.createTempDirectory("test-classpath");
- final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", "executionId");
+ final CodeGenMojo mojo = loadMojo(folder.toFile(), "src/test/resources/classpath", null, "executionId");
/* Perform an initial generation */
mojo.execute();
@@ -159,7 +176,7 @@ public void testSkipRegenerationForClasspathSpecFileWithChange() throws Exceptio
public void testCollapsedSpecProduced() 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();
@@ -172,7 +189,7 @@ public void testCollapsedSpecProduced() throws Exception {
public void testCollapsedSpecAddedToArtifacts() 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();
@@ -187,7 +204,7 @@ public void testCollapsedSpecAddedToArtifacts() throws Exception {
public void testAnyInputSpecMustBeProvided() 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");
mojo.inputSpec = null;
mojo.inputSpecRootDirectory = null;
@@ -201,7 +218,7 @@ public void testAnyInputSpecMustBeProvided() throws Exception {
public void testInputSpecRootDirectoryDoesNotRequireInputSpec() 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");
mojo.inputSpec = null;
mojo.inputSpecRootDirectory = "src/test/resources/default";
@@ -214,14 +231,14 @@ public void testInputSpecRootDirectoryDoesNotRequireInputSpec() throws Exception
assertTrue(hashFolder.resolve("_merged_spec.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);
@@ -234,15 +251,22 @@ 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"));
+ DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
+ session.setLocalRepositoryManager(new SimpleLocalRepositoryManagerFactory().newInstance(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 a2b3a7b26f6d..67a74ce37457 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 @@