How to compile using -Xlint:unchecked in a Maven project? - java

In NetBeans 7.2, I'm having trouble finding how to compile using -Xlint:unchecked in a Maven project. Under an Ant project, you can change compiler flags by going to Project Properties -> Compiling, but Maven projects don't seem to have any such option.
Is there any way to configure the IDE to compile with such flags using Maven?

I guess you can set compiler arguments in your pom.xml. Please refer this http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html
<compilerArgument>-Xlint:unchecked</compilerArgument>

I want to elaborate on #Nishant's answer. The compilerArgument tag needs to go inside plugin/configuration tag. Here is a full example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
</plugins>

This works for me...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
<compilerArgs>
<arg>-Xlint:unchecked</arg> <-------this right here ---->
</compilerArgs>
</configuration>
</plugin>
</plugins>

The pom file information is spot on. I had the additional challenge of building someone else's Maven project in Jenkins and not having access to the pom file repository.
I created a pre-build step to insert the compiler parameter into the pom file after downloading it from git, for example
sed -i 's|/target> *$|/target>\n<compilerArgument>\n-Xlint:deprecation\n</compilerArgument>|' $WORKSPACE/pom.xml

Related

maven: Some input files use unchecked or unsafe operations [duplicate]

In NetBeans 7.2, I'm having trouble finding how to compile using -Xlint:unchecked in a Maven project. Under an Ant project, you can change compiler flags by going to Project Properties -> Compiling, but Maven projects don't seem to have any such option.
Is there any way to configure the IDE to compile with such flags using Maven?
I guess you can set compiler arguments in your pom.xml. Please refer this http://maven.apache.org/plugins/maven-compiler-plugin/examples/pass-compiler-arguments.html
<compilerArgument>-Xlint:unchecked</compilerArgument>
I want to elaborate on #Nishant's answer. The compilerArgument tag needs to go inside plugin/configuration tag. Here is a full example:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testSource>1.8</testSource>
<testTarget>1.8</testTarget>
<compilerArgument>-Xlint:unchecked</compilerArgument>
</configuration>
</plugin>
</plugins>
This works for me...
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
<compilerArgs>
<arg>-Xlint:unchecked</arg> <-------this right here ---->
</compilerArgs>
</configuration>
</plugin>
</plugins>
The pom file information is spot on. I had the additional challenge of building someone else's Maven project in Jenkins and not having access to the pom file repository.
I created a pre-build step to insert the compiler parameter into the pom file after downloading it from git, for example
sed -i 's|/target> *$|/target>\n<compilerArgument>\n-Xlint:deprecation\n</compilerArgument>|' $WORKSPACE/pom.xml

How to include all the files from JRE while bundling

I'm using the javafx-maven-plugin for creating native installer. It all works fine. But inside runtime folder I would like to have all the files inside JRE. but now all the .exe file are missing. Can anyone please tell me how can I achieve this.
following is the pom file I'm using.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<vendor>mos</vendor>
<mainClass>com.smatt.morse.MorseCodeTranslator</mainClass>
</configuration>
</plugin>
</plugins>
</build>
and I'm calling mvn jfx:native cmd.
Thanks

Unable to pass java compiler parameters using maven

As the title says I am unable to pass command line parameters to the java compiler using maven, I am using the maven-compiler-plugin to do it, and accordingly to this (specifically for the compilerArgs option of the pluging) I am using the "latest way" to speficy the arguments passed to the compiler. Well enough talk, more code, this is my maven configuration for the plug-in and I am not sure what am I doing wrong:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
I am following the instructions for the usage of the tool which says that <fork> have to be set to true, and I do not know what am I missing... a little bit of help please?
May or may not be helpful to mention that: I need the parameters argument as specified here because I want to get the name of the arguments in my methods in runtime using reflection; I use the -X argument when calling maven to see the debug and I shows me the "fork" call that it does and I cannot se ANYWHERE the arguments I am passing (maybe I need to enable the plug-in; but I think In this case is automatically enabled since it is not part of any profile, I am not a maven expert so please correct me if I am wrong).
EDIT: I have tried in several ways with and without the dash I have even tried the "old way" to do it:
<compilerArguments>
<parameters />
</compilerArguments>
And:
<compilerArgument>-parameters</compilerArgument>
My mistake: I created the code before modifying my pom file, and ran it using maven to check that is was actually working. After that, I modified my pom to include the -parameters flag. The code had already been compiled without that flag and was not modified after. Therefore, maven saw no changes in the code and did not recompile the file.
SOLUTION execute a mvn clean, delete the compiled classes, delete the target folder, or whatever is necessary to ensure that the files are recompiled.
Please mention, that you always have to write a '-' letter before
the parameters.
Below you can see a configuration for your plugin with some sample
compiler arguments.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<compilerArgs>
<arg>-verbose</arg>
<arg>-Xlint:all,-options,-path</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
From the maven compile plugin main page:
The Compiler Plugin is used to compile the sources of your project. Since 3.0, the default compiler is javax.tools.JavaCompiler (if you are using java 1.6) and is used to compile Java sources. If you want to force the plugin using javac, you must configure the plugin option forceJavacCompilerUse.
I'm guessing the javax.tools.JavaCompiler doesn't works the same way javac does with the -parameters option.
Try forcing the javac use
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<forceJavacCompilerUse>true</forceJavacCompilerUse>
<fork>true</fork>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

What configuration required to run maven project with selenium (TestNG) from command line?

Required Configuration for running maven project from command line.
Very Nice question.
I had also stuck with same question but I have found solution.
Required two maven plug-ins which are given under.
Please make sure your JDK 1.7 and JRE 7 version is configure in your project.
Add this plugin to your pom.xml and follow this step:
1 mvn clean
2 mvn install
3 mvn exec:java
Step 3 is required to run project without specify main class
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>${project.build.sourceEncoding}</encoding>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<executable>${env.JAVA_HOME}/bin/javac</executable>
<fork>true</fork>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.mainClass</mainClass>
</configuration>
</plugin>

FindBugs: How can I run it in Java 5 mode?

When I run FindBugs on my project via Maven, I get lots of these:
Can't use annotations when running in JDK 1.4 mode!
How do I fix that? Couldn't find anything in the manual.
I believe you are missing the targetJdk element in the plugin configuration, like in below snippet.
<reporting>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>2.0.1</version>
<configuration>
<targetJdk>1.5</targetJdk>
</configuration>
</plugin>
</plugins>
</reporting>
Make sure your Maven build plugin is compiling to 1.5, and not 1.4.
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>

Categories