Maven Compile Plugin By Command - java

I have a project in Windows-1254 file encoding and some of files are in UTF-8 encoding.
<properties>
<project.build.sourceEncoding>Windows-1254</project.build.sourceEncoding>
<project.reporting.outputEncoding>Windows-1254</project.reporting.outputEncoding>
<version.plugin.maven.resources>3.1.0</version.plugin.maven.resources>
<functionAppName>az-app-core</functionAppName>
</properties>
I added plugin in pom and compiles correctly with mvn compile.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<id>compile1</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<excludes>
<exclude>**/StringUtil.java</exclude>
<exclude>**/TurkceInputTag_FaceLift.java</exclude>
<exclude>**/TurkceInputTag.java</exclude>
</excludes>
<encoding>Windows-1254</encoding>
</configuration>
</execution>
<execution>
<id>compile2</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<includes>
<include>**/StringUtil.java</include>
<include>**/TurkceInputTag_FaceLift.java</include>
<include>**/TurkceInputTag.java</include>
</includes>
<encoding>UTF-8</encoding>
</configuration>
</execution>
</executions>
</plugin>
But I need to compile project by command not just clean compile but also give all configurations(defined above compile1, compile2 executions) to maven like
maven compile-plugin:compile -Dexecutions/execution1/id=compile1,encoding=Windows-1254,excludes=....
I can't change File types encoding to only UTF-8 or Windows-1254 encoding. I need to compile project using both 2 encoding.
How can maven plugin compiles by command with configurations, encoding,executions etc.?

Unfortunately the command line for Maven is not as flexible as you might like it to be for what you're wanting to achieve. (I have quite a big question about why you're trying to achieve it, and can't just specify that information in the POM as you've demonstrated).
If you look at the goal documentation for maven-compiler-plugin:compile, you'll see that some of the options, like encoding, have a 'user property'. This, prefixed with -D to make it a system property, allow you to configure it from the command line:
mvn <goals/phases> -Dencoding=... -Dmaven.compiler.failOnError=...
But running a goal from the command line will give a single execution, not the set of two that you want. So your options might be:
Run the mvn command twice, with different options on each one. Tricky though as you can't specify inclusions/exclusions.
Split the project into more than one, having different options for encoding in each, and run those from the command line.
Get around whatever limitation it is that is giving you this issue in the first place, and run from the POM as you've defined rather than on the command line.

I second khmarbaise.
All source code files in one project need to have the same encoding. Choose one and convert the other source code files.
EDIT:
You mentioned that you could not convert the files, but unfortunately, you did not tell us why.
Whatever hinders you to do it, you need to solve that issue.
So if your colleagues, managers or customers tell you not to change encoding, then you need to solve this problem by talking to these people, explaining them that a Maven project needs to have one (and just one) source code encoding and convincing them to change that.
Feel free to comment on my answer if I misunderstood you.

Related

VS code Java CheckStyle extension not responding

I'm working on a Maven Java project in VScode and I would like to use https://code.visualstudio.com/docs/java/java-linting to format and auto-correct the checkstyle errors in the source code.
I installed the extension, from command-pallet configured google_checks.xml, set checkstyle version, and then tried to trigger it from the command-palet with 'check code with checkstyle' command.
Nothing happened...
My settings.json looks as follows:
{
"java.configuration.updateBuildConfiguration": "automatic",
"java.checkstyle.configuration": "/google_checks.xml",
"java.checkstyle.version": "8.43"
}
[EDIT]
My pom.xml is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${maven-checkstyle-plugin.version}</version>
<configuration>
<configLocation>sun_checks.xml</configLocation>
</configuration>
<executions>
<execution>
<id>checkstyle</id>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
I forgot to mention that it's a Maven project. I activated the Maven site command, I saw 273 errors reported, then configured the extension to sun_check.xml as well but again no result when I tried to use it.
What's missing in order to trigger this extension?
The problem was a version mismatch "java.checkstyle.version" was configured to "8.43", when I changed it to "8.18" it finally worked.
I follow the tutorial: installing Checkstyle for Java, setting configuration file, then the configuration is applied to .java file immediately:
Deleting the generated settings about checkstyle, opening Command Palette and choose the option Java: Clean Java Language Server Workspace, then try again. OR please refer to goole_checks.xml and check if your code has already met the rules.

Is it possible to pass two arguments in commandlinesArgs?

I am working on a new project and I need to convert several idl files using the idlpp command.
I work under IntelliJ 2020.1 using Maven.
Here is my code (just plugin exec-maven-plugin) :
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<id>generate-source</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>${idlpp_exec}</executable>
<workingDirectory>${gen-idl-dir}</workingDirectory>
<commandlineArgs>${example-idl}</commandlineArgs>
<commandlineArgs>${basic-idl}</commandlineArgs>
<commandlineArgs>${weather-idl}</commandlineArgs>
</configuration>
</execution>
</executions>
</plugin>
It works well on if I place a single commandlineArgs but the problem is that I have several idl files. It is possible to compile everything in a single commandlineArgs or is it impossible ??
Thanks
EDIT
I forgot to specify here is what is behind the calls ${} :
<gen-idl-dir>${project.build.directory}/generated-sources/idl/</gen-idl-dir>
<example-idl>-l java -S "../../../../IDL_Files/Chat.idl"</example-idl>
<basic-types-idl>-l java -S "../../../../IDL_Files/Basic_Types.idl"</basic-types-idl>
<common-types-idl>-l java -S "../../../../IDL_Files/Common_Types.idl"</common-types-idl>
The command <commandlineArgs>${example-idl} ${basic-types-idl}</commandlineArgs> does not work either because of the calls how I could fix that ??
Based on the plugin document, multiple arguments in commandlineArgs are separated by space,
Arguments separated by space for the executed program. For example: "-j 20"
So you should try with:
<commandlineArgs>${example-idl} ${basic-idl} ${weather-idl}</commandlineArgs>
Separate the Arguments using space. Use -j 30 or -j 10 Depending on the spaces you wish to have. This is also how we format in java except they add a % before.
Hope that helps!

How to properly use the server stubs generated from a swagger specification?

I'm using Swagger 2.0 and swagger-codegen (actually the swagger-codegen-plugin for Maven) to specify,document and generate an API, with Java as the target language.
The project is already setup to build the server stubs (JAX-RS) and documentation, and Eclipse recognizes the generated code in the project buildPath.
I'm not sure of what is the proper workflow from here. :-/
I don't think I should modify the generated classes, otherwise my changes would be overwritten whenever I change the swagger spec, an I expect it will change as I think more about the API as the development goes on.
What should I do then? Inherit from the generated classes (which ones?) or include them in my own classes?
There are two steps to the solution here.
Add **/*Controller.java or **/*Impl.java to .swagger-codegen-ignore file. Depending on the language used the default implementation is provided in a *Controller.java or *Impl.java file. Once the default implementation is excluded from generation, you can implement the generated interfaces in your own class. The code in your own class will not get refreshed on mvn clean.
.swagger-codegen-ignore file itself is an auto-generated file hence whatever you add in step 1 gets refreshed when you do a mvn clean. To avoid this keep your version of .swagger-codegen-ignore in your resources folder and add the below plugin to your pom, to copy the file at the start of the Maven lifecycle:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>initialize</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/generated/swagger</outputDirectory>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<includes>
<include>.swagger-codegen-ignore</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
I believe you will need to update the Impl classes, e.g. PetApiServiceImpl.
If you want to skip certain files (e.g. Impl classes) during code regeneration, you can add the files to .swagger-codegen-ignore.

What is priority of parameters passed to maven plugin?

I see that a parameter can be configured in pom.xml or passed in the CLI such as -Dxxxxx=...
My question is if the same parameter is both configured in file pom.xml and passed in the CLI, which will be used by the maven plugin? Is there any document about this priority?
Mostly I believe CLI will override, but this real case shows the opposite.
<plugin>
<groupId>de.saumya.mojo</groupId>
<artifactId>rspec-maven-plugin</artifactId>
<version>1.0.0-beta</version>
<configuration>
<launchDirectory>${project.build.directory}/test-classes</launchDirectory>
<summaryReport>${project.build.directory}/test-Ruby.xml</summaryReport>
<specSourceDirectory>./new_test</specSourceDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
When I ran
mvn test -DspecSourceDirectory=./spec
The plugin still picked the specSourceDirectory in the pom.xml which is ./new_test
I'm using maven 3.0.5, java 7, jruby 1.7.5
Got it resolved: it should be a property instead of a hardcode
<specSourceDirectory>${specSourceDirectory}</specSourceDirectory>
One thing is a plugin's configuration parameter and the other thing is Maven's invocation property (user property). For example, look at Surefire's skip configuration parameter. There is a skip parameter that can be set up by maven.test.skip property. In general these 2 names are independent, so can be either different or the same.
In your case, <specSourceDirectory>${specSourceDirectory}</specSourceDirectory> will be such a latter scenario and will work as you expect.

Encoding issue when building with mvn

I'm currently working on a Maven plugin that uses JAXB. The problem is that whether I launch a clean install in IntelliJ or in a console, I don't get the same results. JAXB is reading an XML file encoded in UTF-8, which contains special characters.
In IntelliJ, these characters are read without any problem.
But in the console, these characters are replaced with '?' (I can see it if I do a sysout).
I found the source of my problem, but I don't really understand it and I don't know how to solve it: when IntelliJ runs mvn, it adds an extra parameter -Dfile.encoding=UTF-8
java -classpath /usr/share/maven/boot/plexus-classworlds-2.4.jar -Dclassworlds.conf=/usr/share/maven/bin/m2.conf -Dmaven.home=/usr/share/maven -Dfile.encoding=UTF-8 org.codehaus.plexus.classworlds.launcher.Launcher clean install
When I run mvn in command line, I can add this extra parameter but it will appear after the class name:
java -classpath /usr/share/maven/boot/plexus-classworlds-2.4.jar -Dclassworlds.conf=/usr/share/maven/bin/m2.conf -Dmaven.home=/usr/share/maven org.codehaus.plexus.classworlds.launcher.Launcher -Dfile.encoding=UTF-8 clean install
In both cases, if I sysout the content of System.getProperty("file.encoding"), I get the same value "UTF-8", but a different behaviour.
Of course I configured my pom.xml correctly using a property like this:
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
I also tried to add a second property named "file.encoding" but it does not help.
My question is: why does the position of this system property change the behaviour of my program, and how can I fix my problem when I run mvn from command line?
Thanks in advance.
Maybe try including this XML in your pom.xml file:
<project>
[...]
<build>
[...]
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.4.3</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</source>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</build>
</project>
As suggested in other questions, the JVM starts with a default file encoding (usually the system file encoding) which is used every time Readers and Writers are used without an explicit encoding. Overriding the system property file.encoding at runtime does not change this behavior, you really have to specify -Dfile.encoding when starting the JVM if you want it to be the default encoding.
To fix my issue, I reused project.build.sourceEncoding in my Maven plugin (I had this problem in a custom plugin) to override file.encoding at runtime, and use this file.encoding to instantiate InputStreamReader and OutputStreamWriter with an explicit encoding as 2nd parameter.
As resources were correctly copied by the maven-resource-plugin in UTF-8, my problem was gone :D.

Categories