swagger-codegen custom generator ClassNotFound - swagger

I'm writing a custom generator for swagger-codegen. When I attempt to run the generator with
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i path/to/swagger.json -l com.my.company.codegen.MyGenerator -o outputlocation
it fails with
Can't load config class with name com.my.company.codegen.MyGenerator
... list of built-in generators...
at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:31)
at io.swagger.codegen.config.CodegenConfigurator.toClientOptInput(CodegenConfigurator.java:286)
at io.swagger.codegen.cmd.Generate.run(Generate.java:186)
at io.swagger.codegen.SwaggerCodegen.main(SwaggerCodegen.java:35)
Caused by: java.lang.ClassNotFoundException: com.my.company.codegen.MyGenerator
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:191)
at io.swagger.codegen.CodegenConfigLoader.forName(CodegenConfigLoader.java:29)
... 3 more
I'm not having trouble with any of the built-in generators.
What I did to get here (following the readme):
cloned the project
mvn package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar meta -o output/myLibrary -n myGenerator -p com.my.company.codegen
and then the above code
I also tried running mvn package again after making my custom generator (which did not make a .jar file anywhere I could find), and tried creating the .jar file myself. Got the same error.
Also FYI, my confusion was definitely increased by some apparent documentation inconsistencies: expected location for my module differs between here and the classname expected here (end of that section). Also, the command for making your own module specifies modules/swagger-codegen-distribution... when I believe it should specify modules/swagger-codegen-cli.... And the guidance in the project readme doesn't seem very congruent with the custom module readme that is generated here.
I don't normally work with Java, so apologies if I'm just missing something super obvious. Thanks in advance for any help!

After trying a bunch of things / internetting, here is what worked:
java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar io.swagger.codegen.SwaggerCodegen generate -i path/to/swagger.json -l com.my.company.codegen.MyCustomCodegenGenerator -o outputlocation
Here are the steps I had to take start to finish to create a custom generator:
git clone from source
cd swagger-codegen
mvn package
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar meta -o output/myLibrary -n myCustomCodegen -p com.my.company.codegen. This will create output/myLibrary and subdirectories, where you should find myCustomCodegenGenerator.java ("Generator" is appended to the class name you specify in the command). You should also be able to find the mustache templates within the resources subdirectory.
Make whatever changes you want to myCustomCodegenGenerator.java and the templates.
cd output/myLibrary
mvn package
cd ../..
Now generate your custom library: java -cp output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar io.swagger.codegen.SwaggerCodegen generate -i path/to/swagger.json -l com.my.company.codegen.MyCustomCodegenGenerator -o outputlocation (building in step 7 should have generated target/myCustomCodegen-swagger-codegen-1.0.0.jar for you)
Notes:
Obviously the cding is based on where I put things, just wanted to be clear on relatively where I was when running commands
If you are just using the default generated base class for your generator (instead of subclassing an existing language), you will get an exception FileNotFound for myCustomCodegen/myFile.mustache -- it's from this optional block which you can just comment out of your custom generator class.
Remember to mvn package your custom module when you make changes

You'll need to include your custom library in the java command. For example:
java -cp path/to/your/jar.com:modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
-jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar \
{args}

Note that if you are attempting to create a generator in Windows and run it from PowerShell, I had to modify #baylee's steps as follows:
mvn install
and
java -cp 'output/myLibrary/target/myCustomCodegen-swagger-codegen-1.0.0.jar;modules/swagger-codegen-cli/target/swagger-codegen-cli.jar' io.swagger.codegen.Codegen -i path/to/swagger.json -l my-language -o outputlocation

Related

How to get the ZFTool working?

The ZFTool isn't working (anymore) on my Debian VM.
$ which zf
/usr/local/bin/zf
$ ls -lia /usr/local/bin/ | grep "zf"
... zf -> /usr/share/.composer/vendor/bin/zf.php
$ cd /usr/share/.composer
$ composer info | grep "zftool"
zendframework/zftool v0.1.0 Utility module for Zend Framework 2 applications.
$ echo $COMPOSER_HOME
/usr/share/.composer
Now when I start it:
$ zf create module Foo
OR
$ $COMPOSER_HOME/vendor/bin/zf.php create module Foo
OR
$ php $COMPOSER_HOME/vendor/bin/zf.php create module Foo
I get this error:
Reason for failure: Invalid arguments or no arguments provided
So I cannot run the ZFTool jobs like config, classmap generate etc.
What is the issues caused by and how to get the ZFTool working?
You're simply providing incorrect arguments as it says. That error is coming from withing ZF2 so you're triggering it, but there's There's no -h option.
try
./vendor/zendframework/zftool/zf.php
or
./vendor/bin/zf.php
no arguments and from within your root directory, you'll get a list of options.

How to re-build jna-4.1.0.jar with linux-s390x specific libjnidispatch.so

How can I rebuild jna-4.1.0.jar file to include the linux-s390x specific libjnidispatch.so file.
This is needed by one of my application and failing on the dependency of this libjnidispatch.so file.
Did try to follow this question: How to use JNAerator with multiple dynamic libraries under one header?
Syntax Used:
java -jar jnaerator-0.11-shaded.jar \
> -arch linux-s390x linux-s390x/libjnidispatch.so \
> -mode jna-3.3.0-jenkins-3.jar \
> -jar jna-3.3.0-jenkins-3_updated.jar
Getting below error:
ERROR: JNAeration failed !
#
# Error parsing arguments :
# -arch linux-s390x linux-s390x/libjnidispatch.so -mode jna-3.3.0-jenkins-3.jar -jar jna-3.3.0-jenkins-3_updated.jar : com.ochafik.lang.jnaerator.JNAerator$CommandLineException: Argument 'linux_s390x' is not one of the expected values :
# linux_x64,
# linux_x86,
# armeabi,
# sunos_x86,
# sunos_sparc,
# darwin_universal,
# win32,
# win64
# Please use -h for help on the command-line options available.
Clone the git repository.
Make sure you have a JDK installed, with Apache ant and native build tools (make, gcc, grep, etc).
Then just run ant native; ant jar.
Note that s390x may not be recognized out of the box, but if you look through the build files for how the other platforms are handled it should be straightforward to add in a switch for s390x (see build.xml and native/Makefile).
If you have a package distribution for a previous JNA version, that package may include the necessary packages and if so, consider submitting them to the JNA project as a github pull request to have them incorporating into the project proper.

spark submit add multiple jars in classpath

I am trying to run a spark program where i have multiple jar files, if I had only one jar I am not able run. I want to add both the jar files which are in same location. I have tried the below but it shows a dependency error
spark-submit \
--class "max" maxjar.jar Book1.csv test \
--driver-class-path /usr/lib/spark/assembly/lib/hive-common-0.13.1-cdh​5.3.0.jar
How can i add another jar file which is in the same directory?
I want add /usr/lib/spark/assembly/lib/hive-serde.jar.
Just use the --jars parameter. Spark will share those jars (comma-separated) with the executors.
Specifying full path for all additional jars works.
./bin/spark-submit --class "SparkTest" --master local[*] --jars /fullpath/first.jar,/fullpath/second.jar /fullpath/your-program.jar
Or add jars in conf/spark-defaults.conf by adding lines like:
spark.driver.extraClassPath /fullpath/firs.jar:/fullpath/second.jar
spark.executor.extraClassPath /fullpath/firs.jar:/fullpath/second.jar
You can use * for import all jars into a folder when adding in conf/spark-defaults.conf .
spark.driver.extraClassPath /fullpath/*
spark.executor.extraClassPath /fullpath/*
I was trying to connect to mysql from the python code that was executed using spark-submit.
I was using HDP sandbox that was using Ambari. Tried lot of options such as --jars, --driver-class-path, etc, but none worked.
Solution
Copy the jar in /usr/local/miniconda/lib/python2.7/site-packages/pyspark/jars/
As of now I'm not sure if it's a solution or a quick hack, but since I'm working on POC so it kind of works for me.
In Spark 2.3 you need to just set the --jars option. The file path should be prepended with the scheme though ie file:///<absolute path to the jars>
Eg : file:////home/hadoop/spark/externaljsrs/* or file:////home/hadoop/spark/externaljars/abc.jar,file:////home/hadoop/spark/externaljars/def.jar
Pass --jars with the path of jar files separated by , to spark-submit.
For reference:
--driver-class-path is used to mention "extra" jars to add to the "driver" of the spark job
--driver-library-path is used to "change" the default library path for the jars needed for the spark driver
--driver-class-path will only push the jars to the driver machine. If you want to send the jars to "executors", you need to use --jars
And to set the jars programatically set the following config:
spark.yarn.dist.jars with comma-separated list of jars.
Eg:
from pyspark.sql import SparkSession
spark = SparkSession \
.builder \
.appName("Spark config example") \
.config("spark.yarn.dist.jars", "<path-to-jar/test1.jar>,<path-to-jar/test2.jar>") \
.getOrCreate()
You can use --jars $(echo /Path/To/Your/Jars/*.jar | tr ' ' ',') to include entire folder of Jars.
So,
spark-submit -- class com.yourClass \
--jars $(echo /Path/To/Your/Jars/*.jar | tr ' ' ',') \
...
For --driver-class-path option you can use : as delimeter to pass multiple jars.
Below is the example with spark-shell command but I guess the same should work with spark-submit as well
spark-shell --driver-class-path /path/to/example.jar:/path/to/another.jar
Spark version: 2.2.0
if you are using properties file you can add following line there:
spark.jars=jars/your_jar1.jar,...
assuming that
<your root from where you run spark-submit>
|
|-jars
|-your_jar1.jar

Using Xbuild with Xamarin.Android (formerly Mono for Android)

We have a Xamarin.Android project that we are trying to build using Jenkins on a Mac. The Solution file contains several different projects, one of which is the MonoDroid project. The MonoDroid Project is dependent upon the other projects in the solution.
The problem that I have is that when I use xbuild to build the solution file, I have no way to use the /t:PackageForAndroid target, since it only is valid for the MD Project File.
Currently in Jenkins, I'm doing it like this:
xbuild MyCoolDroidAp/MyCoolDroidApp.sln /p:Configuration=Release /t:Clean
xbuild MyCoolDroidApp/MyCoolDroidApp.sln /p:Configuration=Release /t:Build
xbuild MyCoolDroidApp/MyCoolDroidProject.csproj /p:Configuration=Release /t:PackageForAndroid
This is working, but it seems to me that there should be a way to eliminate the 3rd step. Does anyone have any insight?
You don't need to use Xamarin.Studio/MonoDevelop to sign & zipalign your APK, you can do that at the command line. I've had luck using rake to compile, sign, and zipalign my APK files. Would something like that work for you?
Failing that, here is a simple Powershell script that you could probably port over real easy:
# First clean the Release target.
msbuild.exe HelloWorld.csproj /p:Configuration=Release /t:Clean
# Now build the project, using the Release target.
msbuild.exe HelloWorld.csproj /p:Configuration=Release /t:PackageForAndroid
# At this point there is only the unsigned APK - sign it.
# The script will pause here as jarsigner prompts for the password.
# It is possible to provide they keystore password for jarsigner.exe by adding an extra command line parameter -storepass, for example
# -storepass <MY_SECRET_PASSWORD>
# If this script is to be checked in to source code control then it is not recommended to include the password as part of this script.
& 'C:\Program Files\Java\jdk1.6.0_24\bin\jarsigner.exe' -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore ./xample.keystore -signedjar
./bin/Release/mono.samples.helloworld-signed.apk
./bin/Release/mono.samples.helloworld.apk publishingdoc
# Now zipalign it. The -v parameter tells zipalign to verify the APK afterwards.
& 'C:\Program Files\Android\android-sdk\tools\zipalign.exe' -f -v 4
./bin/Release/mono.samples.helloworld-signed.apk ./helloworld.apk
Hope this helps.
The consensus around the interwebs seems to be that I am doing this the right way.
Regarding Signing your apk i'm using something like this as a part of my makefile and it works ok:
...
BUILD_DIR = ./builds/$(platform)
KEYSTORE_PATH = your_keystore_pass
STORE_PASS = your_keystore_pass
ANDROID_SDK_PATH = path/to/your/android/sdk/dir
#example ANDROID_SDK_PATH = /Developer/AndroidSDK
RES_APK = my_apk.apk
APK_NAME = my_signed_apk.apk
...
sign:
(cd $(BUILD_DIR); jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore $(KEYSTORE_PATH) -storepass $(STORE_PASS) result.apk $(STORE_PASS))
(cd $(BUILD_DIR); $(ANDROID_SDK_PATH)/tools/zipalign -v 4 result.apk $(APK_NAME))
(cd $(BUILD_DIR);rm result.apk)
(cd $(BUILD_DIR);rm $(RES_APK))
Hope this helps

Compiling java programs from CLI using javac

I have the following directory structure form where I am invoking javac
src/ lib/ build/
Under src:
src/com/xyz/App.java -- contains the main class
src/com/xyz/base/j1.java
src/com/xyz/base/j2.java
src/com/xyz/exceptions/e1.java
src/com/xyz/hibernate/factory/hbf1.java
src/com/xyz/hibernate/helper/hbh1.java
Under lib:
lib/hibernate.jar
lib/commons.jar
At the top level, I am using the following javac command:
javac -verbose -classpath lib/hibernate.jar:lib/commons.jar -d ./build -sourcepath ./src com/xyz/*.java
and I receive the following output
javac: No match
How should the args be passed to javac?
And here is the ANSWER:
javac -verbose -d build -classpath lib/commons.jar:lib/hibernate.jar [complete path for ALL the directories]/*.java
javac won't expand wildcards, that's what your shell does. so when you specify com/xyz/*.java , that will not expand to anything, as those files are under src/ but the shell doesn't know that. If you list out every java file as com/xyz/Foo.java com/xyz/Bar.java etc, it should work.
(note that if you're on windows you'll need ; and not : to seperate classpaths)
Something like this might work:
javac -verbose -classpath build:lib/hibernate.jar:lib/commons.jar -d ./build ./src/com/xyz/base/*java ./src/com/xyz/exceptions/*.java ./src/com/xyz/hibernate/factory/*.java ./src/com/xyz/*.java
I'd not do this other than as an exercise on how to compile from a command line, otherwise use a build tool like ant

Resources