swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

Problems executing swagger-codegen-cli from Maven when using commandlineArgs

Open dk-github opened this issue 11 years ago • 11 comments

Adding swagger-codegen-cli.jar as dependency in exec-maven-plugin causes it to fail with the exception quoted below. This effectively makes it impossible to execute swagger-codegen-cli from maven in the same way as from the command line.

If would be good if swagger-codegen-cli.jar shaded org.codehaus.plexus.util so that it doesn't clash with the version used internally by exec-maven.

The exception generated by maven:

Caused by: org.apache.maven.plugin.PluginContainerException: An API incompatibility was encountered while executing org.codehaus.mojo:exec-maven-plugin:1.3.2:java: java.lang.NoSuchMethodError: org.codehaus.plexus.util.cli.CommandLineUtils.translateCommandline(Ljava/lang/String;)[Ljava/lang/String;
-----------------------------------------------------
realm =    plugin>org.codehaus.mojo:exec-maven-plugin:1.3.2
strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
urls[0] = file:/H:/.m2/repository/org/codehaus/mojo/exec-maven-plugin/1.3.2/exec-maven-plugin-1.3.2.jar
urls[1] = file:/H:/.m2/repository/io/swagger/swagger-codegen-cli/2.1.2/swagger-codegen-cli-2.1.2.jar

dk-github avatar Jun 22 '15 16:06 dk-github

You may be able to work around this by using an exclusion : http://maven.apache.org/guides/introduction/introduction-to-optional-and-excludes-dependencies.html

who avatar Jun 22 '15 16:06 who

Not really, because swagger-codegen-cli.jar is an "uber-jar", i.e. it contains all the dependencies physically inside itself. So telling Mavn not to include a separate plexus utils jar won't help - the plexus classes are already inside the swagger-codegen-cli jar, and they will still clash with the ones in exec-maven-plugin. The only way I see is to shade them when the swagger-codegen-cli.jar is built.

dk-github avatar Jun 22 '15 17:06 dk-github

I added exec-maven-plugin and swagger-codegen-cli dependency, but couldn't reproduce the issue. Could you please provide some small sample to reproduce it?

lugaru1234 avatar Jul 01 '15 12:07 lugaru1234

there is a maven-plugin module for codegen in the develop_2.0 branch - could you use that instead of running the cli?

olensmar avatar Jul 01 '15 12:07 olensmar

@olensmar, thanks for the heads-up - yes a dedicated maven plugin for codegen would be ideal complement to the command line cli jar. And it is definitely a cleaner solution compared to adding an "executable uber-jar" as a dependency, even if all external stuff it includes is shaded.

Unfortunately, my firm's rules allow only maven imports from local repository, which I presume is updated with "release" versions only. Currently I'm stuck with version 2.1.2. Do you plan releasing what's in develop_2.0 branch any time soon?

In the meantime, I will try to use the develop_2.0 maven-plugin from home, just to verify it fits the bill for my purposes.

dk-github avatar Jul 02 '15 15:07 dk-github

@lugaru1234, please could you let me know which versions of exec and swagger-codegen were you using when trying to reproduce? The problem might be specific to the versions I have to use - exec-maven-plugin-1.3.2.jar and swagger-codegen-cli-2.1.2.jar, as seen in the exception above. If this is not reproduced when using newer release versions of respective jar(s), then hopefully all that I need to do to get it working is request updating my firm's repository to those versions. I can still post the sample pom which fails for me with my versions, if this is relevant.

dk-github avatar Jul 02 '15 15:07 dk-github

@dk-github, Yes, I used the same versions. Please post the sample pom, probably it'll help to reproduce the issue

lugaru1234 avatar Jul 02 '15 16:07 lugaru1234

@lugaru1234, I think I figured what might be the difference between our setups.

I am adding swagger-codegen-cli as plugin dependency (reason being that I don't want it to be a dependency on the module itself, and appear on all the classpaths).

If I add it as module dependency, exec plugin does work ok and generates the code, but as per above it effectively adds codegen jar in the application classpath, and is even more likely to produce some version clashes. I suppose I could explicitly exclude it in other app modules, but this is very fragile.

Here's a sample pom which doesn't work for me, with swagger-codegen-cli as plugin dependency:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>test</version>
    <description>swagger-codegen-cli test</description>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <id>swagger-codegen-test</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <mainClass>io.swagger.codegen.SwaggerCodegen</mainClass>
                    <commandlineArgs>
                        generate -i SomeApiSchema.json -l java -o ${project.basedir}/target/generated-sources/swagger
                    </commandlineArgs>
                    <includeProjectDependencies>true</includeProjectDependencies>
                    <includePluginDependencies>true</includePluginDependencies>
                </configuration>
                <dependencies> 
                    <dependency>
                        <groupId>io.swagger</groupId>
                        <artifactId>swagger-codegen-cli</artifactId>
                        <version>2.1.2</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

    <dependencies>
    </dependencies>

</project>

dk-github avatar Jul 03 '15 14:07 dk-github

And here's a sample pom which does work, if I (incorrectly) put swagger-codegen as project dependency. (In both cases I just run mvn generate-sources, as the example doesn't contain anything other than codegen plugin.)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>test</groupId>
    <artifactId>test</artifactId>
    <version>test</version>
    <description>swagger-codegen-cli test</description>
    <packaging>jar</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <id>swagger-codegen-test</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <executable>java</executable>
                    <mainClass>io.swagger.codegen.SwaggerCodegen</mainClass>
                    <commandlineArgs>
                        generate -i SomeApiSchema.json -l java -o ${project.basedir}/target/generated-sources/swagger
                    </commandlineArgs>
                    <includeProjectDependencies>true</includeProjectDependencies>
                    <includePluginDependencies>true</includePluginDependencies>
                </configuration>                
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-codegen-cli</artifactId>
        <version>2.1.2</version>
        </dependency>
    </dependencies>
</project>

dk-github avatar Jul 03 '15 14:07 dk-github

Thank you for providing the example pom. I reproduce the issue. I think the problem is in command line arguments. Please try to use

<arguments>
    <argument>generate</argument>
    <argument>-i</argument>
    <argument>SomeApiSchema.json</argument>
    <argument>-l</argument>
    <argument>java</argument>
    <argument>-o</argument>
    <argument>${project.basedir}/target/generated-sources/swagger</argument>
</arguments>

instead of

<commandlineArgs>
    generate -i SomeApiSchema.json -l java -o ${project.basedir}/target/generated-sources/swagger
</commandlineArgs>

lugaru1234 avatar Jul 03 '15 16:07 lugaru1234

Right, so this conflict only occurs if the arguments are passed as a single string and have to be parsed. If they are supplied in separate elements, the parsing code is not invoked and there's no conflict.

Thanks a lot, this is definitely a better workaround than adding as project dependency.

So, to summarize for the records:

  • a dedicated maven plugin will soon be part of swagger release, that should be used when running codegen from maven
  • in the meantime, swagger-codegen-cli can be used via exec-maven, however care should be taken to not supply arguments as a single command line

Thanks guys

dk-github avatar Jul 03 '15 17:07 dk-github