VS code Java CheckStyle extension not responding - java

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.

Related

Maven Compile Plugin By Command

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.

Different behavior of gwt-maven-plugin in IDE and commandline

I am using the gwt-maven-plugin and since recently I got strange errors in my IDE (eclipse oxygen). After researching it I realize that the gwt-maven-plugin is behaving differently when run in eclipse or on commandline.
On commandline everything is ok and the gwt:css creates an interface with the correct visisbility (public). But when I run it in eclipse the public is missing and thus I get all the errors in eclipse since the classes can not access the interface anymore. I am using gwt 2.6.1, JDK 1.8.0 (but build itself is with 1.6) and maven 3.5.2.
Any ideas what is causing this?
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>gwt-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test</goal>
<goal>css</goal>
<goal>generateAsync</goal>
<goals>
</execution>
</executions>
<configuration>
<skip>${gwt.skipcompile.config}</skip>
<inplace>true</inplace>
<module>${gwt.module.config}</module>
<runTarget>Config.html</runTarget>
<hostedWebapp>${webappDirectory}</hostedWebapp>
<extraJvmArgs>-Xmx1024M -Xss1024k</extraJvmArgs>
<compileReport>true</compileReport>
<cssFiles>
<cssFile>MyCss.css</cssFile>
</cssFiles>
//And the code is generated like this (eclipse):
interface MyCss extends CssResource ...
//in command line
public interface MyCss extends CssResource ...
The gwt-maven-plugin delegates to GWT InterfaceGenerator and it doesn't add the public modifier. You can see the git-log and confirm that this has been always the case (already reported sometimes). So, you cannot be executing the plugin goal from the command line, you should be using something different. For example, you might have a script that adds the public modifier using awk (or something similar), and this is not executed from eclipse.
Side note: You really should upgrade GWT and gwt-maven-plugin.

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.

maven generate web service in java

I'm new to maven, and I want to use maven to generate the java code from a wsdl file (using the wsimport plugin ?), which is in my project (not in a url).
I think the wsdl file should be somewhere in myprojet/src/main/resources, and the generated sources in myproject/target/generatedsources/ws.
If the choice of these places ok ? If so, how can I create the sources from the wsdl to the designated folder using maven ? Can I choose the java package name of the sources ? Should I ? Should I then make a jar file out of the sources ? How can I make sure that the generated sources are accessible for the compilation (in the classpath) ?
thank you.
There are several ways to do this with the wsimport plugin, but most examples would require more knowledge of your pom.xml
Apache CXF is a popular free plugin for this exact purpose
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>${cxf.version}</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/src/main/resources/myService.wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
NOTE: You can definitely make a jar out of the generated sources, but this would be a separate entry inside your POM
check out Apache's Axis2 -- it's designed specifically to help generate code based on an WSDL for web services. Basically it's exactly what you are looking for:
http://axis.apache.org/axis2/java/core/
EDIT: To expand, I typically will run the wsdl2java(.bat or .sh) file and point it to the wsdl (either on the web or local copy) and it generates everything else. It can even package it all into a jar for you (my preferred method because it's "cleaner"). You likely won't have any need to go into the generated code and change anything, you just end up adding that jar to your classpath then import like any other library and use it.

Categories