Running main class parameters - intellij-idea

I am using intelliJ and I ran the main method of my java class and here is what i got for an output
usage: MyJavaClass
-comment <arg> comments for generated patch
-force force overwrite of merge conflicts. Defaults to false.
-patch <arg> output file for generated patch
-source <arg> Source name (required)
-target <arg> Target name (required)
-v verbose output. Defaults to false.
-DCLIENT_CONF Location for client configuration settings
Process finished with exit code 255
I think I am supposed to enter 2 databases names(local) because I want to transfer information from the first to the second,but how do I pass arguments cause I get the screen above,thanks

Use the Run | Edit Configurations... menu item, find the configuration for MyJavaClass and specify the arguments in the "Program arguments" field.

In Maven projects you could use the exec-maven-plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.5.0</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.path.MyJavaClass</mainClass>
<arguments>
<argument>-source</argument>
<argument>-target</argument>
</arguments>
</configuration>
</plugin>

Related

Annotation needed from swagger codegen

I need a way to annotate my openapi 3 specification so that swagger-codegen will add an annotation to my java class, such as #JsonIgnoreProperties(ignoreUnknown = true)
Is that possible?
TIA!
It seems like you could take advantage of mustache templates. Extract mustache files for required language from code-gen jar file and edit the needed class template file. Then, generate client code with -t pathToTemplates flag like so:
java -jar swagger-codegen-cli-2.3.1.jar generate
-t C:\SwaggerTemplates
-i C:\Swagger.json
-l csharp
-o C:\Output
Yes, it is possible.
Here are the steps that worked for me -
Download the swagger-code-gen source code either from github or get the zip. I have version 2.3.1 and downloaded from swagger-code-gen 2.3.1
For Java, update AbstractJavaCodegen.java file and add the 2 lines in processOpts() method:
importMapping.put("JsonIgnoreProperties","com.fasterxml.jackson.annotation.JsonIgnoreProperties");
// import JsonIgnoreProperties if ApiModel is imported importMapping.put("io.swagger.annotations.ApiModel","com.fasterxml.jackson.annotation.JsonIgnoreProperties");
Save the file and mvn clean install to generate the swagger-code-gen-cli-2.3.1 in target directory
Now extract the "pojo.mustache" and any other required file from the above cli jar (located in this path - "swagger-codegen-2.3.1\modules\swagger-codegen-cli\target\swagger-codegen-cli-2.3.1.jar\Java\") to a directory (e.g. spring_template)
Add the line "#JsonIgnoreProperties(ignoreUnknown = true)" below #ApiModel in the "pojo.mustache" file
Now execute the command with the built cli jar and spring_template in path:
java -jar swagger-codegen-cli-2.3.1.jar generate --additional-properties apiPackage=com.xxx.xxx.api,modelPackage=com.xxx.xxx.model,delegatePattern=true,useTags=true,configPackage=com.xxx.xxx.configuration,basePackage=com.xxx.xxx -o app -l spring -i your_swagger_yaml_file.yaml -t spring_template
This should build and generate pojo/ model classes with #JsonIgnoreProperties(ignoreUnknown = true) and with import com.fasterxml.jackson.annotation.JsonIgnoreProperties in the class.
maven has plugin for swagger
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<executions>
<execution>
<id>java-codegen</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>jaxrs-spec</language>
<templateDirectory>${project.build.directory}/swagger-templates</templateDirectory>
<inputSpec>${basedir}/src/main/resources/v1/swagger.yaml</inputSpec>
<output>${project.build.directory}/swagger-codegen</output>
<apiPackage>com.example.api.v1</apiPackage>
<modelPackage>com.example.api.v1.dto</modelPackage>
<modelNameSuffix>DTO</modelNameSuffix>
<configOptions>
<additional-properties>generateModelBuilders=true,useJackson=true,sortParamsByRequiredFlag=false,useJacksonJsonIgnoreUnknownProperties=true</additional-properties>
<dateLibrary>java8</dateLibrary>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
if we are using swagger-codegen-maven-plugin to generate code then
<additional-properties>generateModelBuilders=true,useJackson=true,sortParamsByRequiredFlag=false,useJacksonJsonIgnoreUnknownProperties=true</additional-properties>
will generate DTO with #JsonIgnoreProperties(ignoreUnknown = true)
Using swagger-codegen v2.4.20 or higher, you can achieve it adding the following configuration to your pom.xml
<additional-properties>ignoreUnknownJacksonAnnotation=true</additional-properties>
Example
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.4.20</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<language>java</language>
<inputSpec>${sourceFile}</inputSpec>
<output>${outputFolder}</output>
<modelPackage>${model.package}</modelPackage>
<generateApis>false</generateApis>
<generateApiTests>false</generateApiTests>
<generateApiDocumentation>false</generateApiDocumentation>
<generateSupportingFiles>false</generateSupportingFiles>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<configOptions>
<additional-properties>ignoreUnknownJacksonAnnotation=true</additional-properties>
<sourceFolder>.</sourceFolder>
<java8>true</java8>
<dateLibrary>legacy</dateLibrary>
<serializableModel>true</serializableModel>
<hideGenerationTimestamp>true</hideGenerationTimestamp>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
The other solutions didn't work for me for whatever reason, but this did:
<configuration>
<configOptions>
<additionalModelTypeAnnotations>#com.fasterxml.jackson.annotation.JsonIgnoreProperties(ignoreUnknown = true)</additionalModelTypeAnnotations>
</configOptions>
</configuration>
Credit to this thread: https://github.com/OpenAPITools/openapi-generator/issues/3438

Maven: copying directories using exec plugin

I'm using Maven 3.0.3. I'm having trouble using the Maven exec plugin to copy the contents of one directory to another. Sadly, when I include this plugin in my pom.xml …
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<configuration>
<executable>cp</executable>
<arguments>
<argument>-r</argument>
<argument>web-app/*</argument>
<argument>src/main/webapp</argument>
</arguments>
</configuration>
<executions>
<execution>
<phase>verify</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
It isn't working. I get the error below …
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.1.1:exec (default-cli) on project jx: Result of /bin/sh -c cd /Users/davea/Documents/workspace/mycoUSA2/Technology/nna/myco2usa/jx && cp -r 'web-app/*' src/main/webapp execution is: '1'. -> [Help 1]
Does anyone know how I can modify my plugin config to copy the contents of one directory to another? Thanks, - Dave
If you are using bash, try the following:
<executable>bash</executable>
<arguments>
<argument>-c</argument>
<argument>cp -r web-app/* src/main/webapp</argument>
</arguments>
This spawns a new bash and gives it the command cp -r web-app/* src/main/webapp to execute.
You can also test if it works for you by inputting this into a normal Terminal window first:
bash -c "cp -r web-app/* src/main/webapp"
Note that the " signs do make a difference as the exec-maven-plugin does insert them automatically, thus they are not included in the <argument>-tag.
Note the command it ran. From the error output:
cp -r 'web-app/*' src/main/webapp
Note in particular the 'web-app/*' file it has tried to copy. Because it has quoted this argument the cp command is looking for a specific file with the name * in the web-app directory. Because you don't have a file with this name it has exited with the error code 1.
The maven-resources-plugin has a goal designed to perform this task. Why not give it a try? It would have the added benefit of making your build platform independent.
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${basedir}/src/main/web-app</outputDirectory>
<resources>
<resource>
<directory>web-app</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
mvn -X might be more revealing
Many people would use the maven-antrun-plugin and script this in ant so as to get a portable solution.

Get name of maven pom file

I am running a Maven (2) release build with with: mvn -f release.xml clean deploy
and want to get the currently running pom file name (release.xml) into a property or mojo.
Is it possible?
well you can use the property
${env.MAVEN_CMD_LINE_ARGS}
in your pom or a filtered resource, which will expand to something like
clean install -Paxis -Dmaven.test.skip -f mypom.xml -pl util
however, if you just want the mypom.xml part, you're going to have to do some scripting, which is not supported out of the box in maven. Common solutions are either maven antrun plugin or gmaven (groovy) plugin. Here's a way to do it in gmaven:
<plugin>
<groupId>org.codehaus.groovy.maven</groupId>
<artifactId>gmaven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
System.out.println(
System
.getenv("MAVEN_CMD_LINE_ARGS")
.replaceFirst( /.*\-f\s+(\S+).*/ , 'POM File: $1')
);
</source>
</configuration>
</execution>
</executions>
</plugin>
Edit: as you want it in a property, either use System.setProperty or write directly to project.properties

Is it possible to single out and run a specific goal bound to a maven phase?

Updated to (hopefully) clarify: If a goal is defined to run during a given phase, is it possible to run the individual goal without running thru all phases. In other words would it be possible to run the antrun:run goal (which is defined as part of the install phase below) without getting dependencies, generate-resources, compiling, testing, package, etc?
I'm using the antrun plugin to create a zip file during the package phase and to delete and copy some files during the install phase. I understand how to run single maven plugin goals, for example: mvn antrun:run. However, is there a way to run a specific execution's goal? Something like mvn antrun:run:execution-id, or mvn phase:antrun:run?
Basically, I'd be nice if I can tell maven to do nothing else but run the ant tasks defined below inside the deploy phase, for example. It's kind of tedious having to wait for maven to go thru all the phases just to check if the ant tasks in the deploy phase are working correctly.
<executions>
<!-- create zip file -->
<execution>
<id>create-zip</id>
<phase>package</phase>
<configuration>
<tasks>
...create zip...
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
<!-- do some other stuff -->
<execution>
<id>copy-files</id>
<phase>install</phase>
<configuration>
<tasks>
...delete some files, copy some files ...
</tasks>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
In other words would it be possible to run the antrun:run goal (which is defined as part of the install phase below) without getting dependencies, generate-resources, compiling, testing, package, etc?
No it's not. While you can configure a plugin (with a <configuration> section under the <plugin> element) and call in on the command line, you can't invoke a specific executionid (and consequently the <configuration> specific to an <execution>).
The only solution in your case would be to declare the antrun plugin in a profile, let's say my-profile, to duplicate the following part of the configuration to configure the plugin in this profile:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<configuration>
<tasks>
... delete some files, copy some files ...
</tasks>
</configuration>
</plugin>
and to call with the right active profile:
mvn antrun:run -Pmy-profile
Try the exec maven plugin...
For ex:
When you run jboss with maven, you can't see the jboss console output, but I need it to display, so what I did is I wrote a java file that reads in server.log(the server console output) as it changes to display the changes so it appears that jboss console is actually showing(a bit hack-ish but working). So I come to the point of answering your question, during the pre-integration-test I executed a java goal which starts my java program. Here is how , using execute plugin of course :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>console-start</id>
<phase>pre-integration-test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.eclipse.console.Main</mainClass>
</configuration>
</plugin>
You just run the install and it executes during the pre-integration-test, however if you just want to execute something like java, use the execute plugin. Sorry if the answer not appropriate, I didn't have the patience to read your question in details, my work hours are over .. cheers

Maven maven-exec-plugin multiple execution configurations

Is it possible to invoke a maven-exec-plugin (or any other plugin's) execution by its id from the command line?
Let's say my pom.xml file looks like this:
<project>
[...]
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>foo</id>
<goals>
<goal>exec</goal>
</goals>
<phase></phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>foo</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>bar</id>
<goals>
<goal>exec</goal>
</goals>
<phase></phase>
<configuration>
<executable>echo</executable>
<arguments>
<argument>bar</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
[...]
</project>
Now is it possible to call
mvn exec:exec
with some added magic to run execution "foo"?
For the curious there is an alternative solution using profiles available here:
http://www.mail-archive.com/user#mojo.codehaus.org/msg00151.html
It is now possible, starting from Maven 3.3.1: see improvement MNG-5768 and Maven 3.3.1 release notes
You can call a specific execution configuration with this syntax:
mvn exec:exec#foo
No, it's not possible. Executions are for binding to the lifecycle (i.e. they are not designed to be invoked on the command line). So you'll have to use the profile trick described in the link that you provided.
Not mentioned here is that, as of Maven 2.2.0, if you give an execution of any plugin the id "default-cli", then when you run that plugin from the command line, that configuration is used. You're limited to only one default execution of each plugin, but it's a start.
I think if you write execute the goal:
org.codehaus.mojo:exec-maven-plugin:¿Version?:exec
it worked for me in eclipse maven plugin.