swagger-codegen-maven-plugin best practices - maven

I'm using the swagger-codegen-maven-plugin within my pom to generate a client. Here is my plugin info:
<plugins>
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.9</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api.json</inputSpec>
<language>java</language>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Using:
java version "1.8.0_221"
Apache Maven 3.6.1
I had many errors when running mvn compile, as the package names could not be found in my repo e.g.
[ERROR]
//target/generated-sources/swagger/src/main/java/io/swagger/client/model/.java:[18,23]
package com.google.gson does not exist
To overcome the errors and enable a successful mvn compile, I have had to name the missing packages within my pom's dependencies e.g.
<dependencies>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
I don't believe this is best practice, as I have to specify the version myself without knowing the actual version that the source has used.
My question is, what is the best practice for generating a client using maven when there are packages which can't be resolved by the swagger-codegen-plugin? Alternatively, please advise if the resolution I have found to overcome the issue is actually the best practice in this scenario?
Thanks.

Related

Install plugin generatePom produces error

We pre-package a dependency with the application,and use Maven install plugin to inject into the classpath.
<dependencies>
<dependency>
<groupId>artifactXXX</groupId>
<artifactId>artifactXXX</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
In the install:
<executions>
<execution>
<id>install-artifactXXX</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>artifactXXX</groupId>
<artifactId>artifactXXX</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${basedir}/jars/artifactXXX.jar</file>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
However, during clean install Maven still tries to download the POM for the dependent JAR from Artifactory and produces a POM not found error.
What is to be done here?
Dependency resolution happens before <build> proccessing. So, at the time Maven processes the <dependencies> section the artifact hasn't been installed yet.
You have to perform install:install-file manually on the command line at least once before your POM build (and you have to be aware of that, if using a -SNAPSHOT version, the dependency installed by the previous build is used in the current build).
Furthermore, such a configuration doesn't comply with the declarative nature of Maven POMs. It's like trying to grow a plant from seeds of the plant that is to be grown.

Install a 3rd-party dependency with maven before dependencies are resolved

I would like to deploy a maven project that depends on a 3rd-party jar, located in a "lib/" directory that is distributed with my project source.
The usual way to do this (as explained in other answers) is to have the user install the jar into a local maven repository before building the project, by typing a command such as mvn install:install-file at the shell.
This manual solution won't do for deployment, however (because requiring users to manually install dependencies is so 1998...). So I thought I'd specify an install-file goal in my pom.xml:
<dependency>
<groupId>edu.my.id</groupId>
<artifactId>myartifact</artifactId>
<version>1.0</version>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-jar-lib</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>edu.my.id</groupId>
<artifactId>myartifact</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${project.basedir}/lib/myartifact.jar</file>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
The problem is that maven attempts to resolve dependencies before install-file runs, even when I specify it to run as early as the validate phase. It thus complains that it cannot find the dependency.
This question has been asked before, and a few unpleasantly complicated solutions have been offered such as these:
Run maven-install-plugin during the clean phase and require users to run mvn clean before building the artifact.
Set up multiple modules, as described here.
Use <systemPath> to load the jar as a system library. (This doesn't meet my requirements, because I am using maven-dependency-plugin to copy dependency jars into the deployed application directory, which ignores system libraries.)
Write a custom plugin.
None of these satisfy me. This seems like a routine task any build system encounters. There must be a simpler way to install dependencies with maven.
The solution that worked for me was to use both install-file and <systemPath>.
<dependency>
<groupId>edu.my.id</groupId>
<artifactId>myartifact</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/myartifact.jar</systemPath>
</dependency>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-jar-lib</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>edu.my.id</groupId>
<artifactId>myartifact</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<file>${project.basedir}/lib/myartifact.jar</file>
<generatePom>true</generatePom>
</configuration>
</execution>
</executions>
This way, when Maven compiles the project, it resolves the 3rd-party dependency as a system library. But it also installs the library into the local maven repository, so that maven-dependency-plugin's copy-dependencies goal is able to find it.

Including third party AMP in main project in Alfresco Maven SDK, especially WCMQS

This is the scenario, I have a group of a AMPs, some developed by myself, and other developed by other developer/vendors.
If I am not wrong, using the Maven SDK I can develop and run only one specific AMP at a time.
What steps can be taken to have an external AMP being deployed along with the main project AMP at start up which is when running mvn integration-test -Pamp-to-war.
In particular I am interested in having Alfresco load the wcmqs module.
Assuming you already have the external amps available to maven (either because their're on Maven Central repo or because they're installed locally), you simply add the external amps as dependencies in your amp project. E.g.:
<dependency>
<groupId>org.sharextras</groupId>
<artifactId>javascript-console-repo</artifactId>
<version>0.6.0</version>
<type>amp</type>
</dependency>
You also must configure the maven dependency plugin. You can do it in a profile so it can be turned on or off depending on your needs:
<profiles>
<profile>
<id>unpack-deps</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack-amps</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeTypes>amp</includeTypes>
<outputDirectory>${alfresco.client.war.folder}</outputDirectory>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.alfresco.maven.plugin</groupId>
<artifactId>maven-amp-plugin</artifactId>
<version>3.0.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</profile>
</profiles>
This way, you can start the main project amp plus its dependencies with the following command:
mvn integration-test -Pamp-to-war -Punpack-deps
For a complete pom.xml example see: https://github.com/douglascrp/alfresco-value-assistance/blob/master/alfresco-value-assistance-repo/pom.xml

Maven and adding JARs to system scope

I have a JAR in my Android project and I want it to be added to final APK.
Okay, here I go:
<dependency>
<groupId>com.loopj.android.http</groupId>
<artifactId>android-async-http</artifactId>
<version>1.3.2</version>
<type>jar</type>
<scope>system</scope>
<systemPath>${project.basedir}/libs/android-async-http-1.3.2.jar</systemPath>
</dependency>
But when I am running mvn package I am getting a warning:
[WARNING] Some problems were encountered while building the effective model for **apk:1.0
[WARNING] 'dependencies.dependency.systemPath' for com.loopj.android.http:android-async-http:jar should not point at files within the project directory, ${project.basedir}/libs/android-async-http-1.3.2.jar will be unresolvable by dependent projects # line 36, column 25
And in the final APK there are no JARs.
How do I fix that?
I don't know the real reason but Maven pushes developers to install all libraries (custom too) into some maven repositories, so scope:system is not well liked, A simple workaround is to use maven-install-plugin
follow the usage:
write your dependency in this way
<dependency>
<groupId>com.mylib</groupId>
<artifactId>mylib-core</artifactId>
<version>0.0.1</version>
</dependency>
then, add maven-install-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>install-external</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/lib/mylib-core-0.0.1.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.mylib</groupId>
<artifactId>mylib-core</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
pay attention to phase:clean, to install your custom library into your repository, you have to run mvn clean and then mvn install
You will need to add the jar to your local maven repository. Alternatively (better option) specify the proper repository (if one exists) so it can be automatically downloaded by maven
In either case, remove the <systemPath> tag from the dependency
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
Try this.
System scope was only designed to deal with 'system' files; files sitting in some fixed location. Files in /usr/lib, or ${java.home} (e.g. tools.jar). It wasn't designed to support miscellaneous .jar files in your project.
The authors intentionally refused to make the pathname expansions work right for that to discourage you. As a result, in the short term you can use install:install-file to install into the local repo, and then some day use a repo manager to share.
Try this configuration. It worked for me:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<warSourceDirectory>mywebRoot</warSourceDirectory>
<warSourceExcludes>source\**,build\**,dist\**,WEB-INF\lib\*,
WEB-INF\classes\**,build.*
</warSourceExcludes>
<webXml>myproject/source/deploiement/web.xml</webXml>
<webResources>
<resource>
<directory>mywebRoot/WEB-INF/lib</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>mySystemJar1.jar.jar</include>
<include>mySystemJar2.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
Use a repository manager and install this kind of jars into it. That solves your problems at all and for all computers in your network.
mvn install:install-file -DgroupId=com.paic.maven -DartifactId=tplconfig-maven-plugin -Dversion=1.0 -Dpackaging=jar -Dfile=tplconfig-maven-plugin-1.0.jar -DgeneratePom=true
Install the jar to local repository.
Thanks to Ging3r i got solution:
follow these steps:
don't use in dependency tag. Use following in dependencies tag in pom.xml file::
<dependency>
<groupId>com.netsuite.suitetalk.proxy.v2019_1</groupId>
<artifactId>suitetalk-axis-proxy-v2019_1</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.netsuite.suitetalk.client.v2019_1</groupId>
<artifactId>suitetalk-client-v2019_1</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>com.netsuite.suitetalk.client.common</groupId>
<artifactId>suitetalk-client-common</artifactId>
<version>1.0.0</version>
</dependency>
use following code in plugins tag in pom.xml file:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<executions>
<execution>
<id>suitetalk-proxy</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/lib/suitetalk-axis-proxy-v2019_1-1.0.0.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.netsuite.suitetalk.proxy.v2019_1</groupId>
<artifactId>suitetalk-axis-proxy-v2019_1</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>suitetalk-client</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/lib/suitetalk-client-v2019_1-2.0.0.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.netsuite.suitetalk.client.v2019_1</groupId>
<artifactId>suitetalk-client-v2019_1</artifactId>
<version>2.0.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
<execution>
<id>suitetalk-client-common</id>
<phase>clean</phase>
<configuration>
<file>${basedir}/lib/suitetalk-client-common-1.0.0.jar</file>
<repositoryLayout>default</repositoryLayout>
<groupId>com.netsuite.suitetalk.client.common</groupId>
<artifactId>suitetalk-client-common</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<generatePom>true</generatePom>
</configuration>
<goals>
<goal>install-file</goal>
</goals>
</execution>
</executions>
</plugin>
I am including 3 jars from lib folder:
Finally, use mvn clean and then mvn install or 'mvn clean install' and just run jar file from target folder or the path where install(see mvn install log):
java -jar abc.jar
note: Remember one thing if you are working at jenkins then first use mvn clean and then mvn clean install command work for you because with previous code mvn clean install command store cache for dependency.
Following this thread I was able to configure the install plugin to load my custom jar, but the plugin was not seeing my configuration when running a mvn install
I'm using the base maven-install-plugin:2.5.2 using the maven:3.6.3-jdk-8 docker image.
I don't fully understand this note in the documentation (at the end of the section), but it seems that you can give the phase goal an execution id forcing it to use your configuration:
Note: Configurations inside the element used to differ from those that are outside in that they could not be used from a direct command line invocation because they were only applied when the lifecycle phase they were bound to was invoked. So you had to move a configuration section outside of the executions section to apply it globally to all invocations of the plugin. Since Maven 3.3.1 this is not the case anymore as you can specify on the command line the execution id for direct plugin goal invocation. Hence if you want to run the above plugin and it's specific execution1's configuration from the command-line, you can execute:
mvn myqyeryplugin:queryMojo#execution1
My final working docker command:
docker run -it --rm --name parser -v "$(shell pwd)":/usr/src/parser -w /usr/src/parser maven:3.6.3-jdk-8 mvn -X install:install-file#install-my-jar-file
Where install-my-jar-file is my executions id <execution><id>install-my-jar-file</id>...

using QueryDSL in osgi

I have been trying to use querydsl in a project which is an osgi bundle.
my pom.xml has the following dependencies:
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.mysema.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>2.5.0</version>
</dependency>
As well as the plugin
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>maven-apt-plugin</artifactId>
<version>0.3.2</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.mysema.query.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.ops4j</groupId>
<artifactId>maven-pax-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<!-- | the following instructions build a simple set of public/private
classes into an OSGi bundle -->
<configuration>
<instructions>
<Import-Package>com.mysema.query.jpa,*</Import-Package>
<Export-Package>com.mypackage.package.*;version="${project.version}"</Export-Package>
</instructions>
</configuration>
</plugin>
Still when I try to start the bundle I get:
Error executing command: Unresolved constraint in bundle com.mypackage.package [163]: Unable to resolve 163.0: missing requirement [163.0] package; (&(package=com.mysema.query.jpa)(version>=2.5.0)(!(version>=3.0.0)))
I was using an older version of querydsl but apparently they fixed some stuff about osgi recently so I upgraded. The problem persists.
What I am missing for querydsl to work inside osgi?
Installing each dependency by hand will be a pain, but AFAIK there's nothing that will take a maven artifact and chain back of all dependencies - this would fail as where would it stop?
You could end up with every version of every logging framework (even if you had pax-logging installed), or the wrong implementation.
Alas in maven's case there's currently no way of applying semantic versioning or higher level requirement and capability. (Though BND (maven-bundle-plugin, bndtools) makes some sensible assumptions at a code level)
Karaf features (see the PDF manual in distribution's ${KARAF_HOME}) can do a lot to alleviate this but it can take some work to setup. There's a(t least) couple of ways to generate features files;
Use the features-maven-plugin
Use the maven-build-helper plugin to publish an XML file that you handcraft (laborious but you can maintain versions using resource filtering).

Resources