Maven install does not encode in UTF-8 even if configured - java

Hi I have a problem with the encoding of my project.
When I run JUnit tests from eclipse, there are no failures. The problem is when I do maven > clean maven > install, one of the tests fails.
I have this string: "ADMINISTRACIÓN", and it's fine when i run the JUnit from eclipse, but I've printed the variable and when maven does the tests, the value of this string is: "ADMINISTRACI�N".
I've changed every property I could find of encoding in eclipse to UTF-8.
-Configured the pom this way:
(...)
<project>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
(...)
</properties>
</project>
(...)
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
(...)
But the output is the same. I have a coworker that has the same project than me, and the same eclipse client and config, and her maven tests print accents with no trouble.
Any further ideas?
Thanks a lot!

Try run your build with:
mvn -DargLine=-Dfile.encoding=UTF-8 clean insall
if help, you can configure surefire in project:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Dfile.encoding=${project.build.sourceEncoding}</argLine>
</configuration>
</plugin>
Problem may occur because System.out use default system encoding, you can change this be setting file.encoding java property.

I tried all settings mentioned in this post to build my project successfully however that didn't work for me. At last I was able to build my project successfully with mvn -DargLine=-Dfile.encoding=UTF-8 clean insall command.

Related

Passing parameter for mvn test from pom

I have a springboot project with tests
there are few parameters(for ex passwords, pins etc) i would like to pass for mvn tests, I know this can be done with -D option from the cli. Can these values be passed from pom.
Below didnt seem to work, i guess this is for execution and not for compilation
<properties>
<someproperty> abcd </someproperty>
</properties>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>#{argLine} -Dsomeproperty=someValue </argLine>
</configuration>
</plugin>
This did the trick.
Answering my question in case someone else needs this

How to exclude some directory in maven command line for test? [duplicate]

Something like the following.
I would like a way to skip my dao tests in surefire. Trying to avoid overhead of defining Suites.
With CI I'd like to have one nightly that runs all tests and another 5 minute poll of SCM that runs only 'fast' tests.
mvn -DskipPattern=**.dao.** test
Let me extend Sean's answer. This is what you set in pom.xml:
<properties>
<exclude.tests>nothing-to-exclude</exclude.tests>
</properties>
<profiles>
<profile>
<id>fast</id>
<properties>
<exclude.tests>**/*Dao*.java</exclude.tests>
</properties>
</profile>
</profiles>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>${exclude.tests}</exclude>
</excludes>
</configuration>
</plugin>
Then in CI you start them like this:
mvn -Pfast test
That's it.
Sure, no problem:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.6</version>
<configuration>
<excludes>
<!-- classes that include the name Dao -->
<exclude>**/*Dao*.java</exclude>
<!-- classes in a package whose last segment is named dao -->
<exclude>**/dao/*.java</exclude>
</excludes>
</configuration>
</plugin>
Reference:
Maven Surefire Plugin > Inclusions and Exclusions of Tests
(The excludes can not be configured via command line, so if you want to turn this behavior on conditionally, you will have to define a profile and activate that on the command line)
It is possible to exclude tests using the commandline; using ! to exclude.
Note: I'm not sure but possibly needs 2.19.1 or later version of surefire to work.
Examples:
This will not run TestHCatLoaderEncryption
mvn install '-Dtest=!TestHCatLoaderEncryption'
Exclude a package:
mvn install '-Dtest=!org.apache.hadoop.**'
This can be combined with positive filters as well. The following will run 0 tests:
mvn install '-Dtest=Test*CatLoaderEncryption,!TestHCatLoaderEncryption'
See the Maven Surefire docs.

mvn clean install do not use last .class version

It seems Maven keep using use an old version of my code when packaging a war archive.
I build my war using a simple "mvn clean install".
I deleted the /target folder by hand and checked that there were no .class elsewhere in my project (as described here).
Each time I check the generated archive, an old version of my code has been packaged inside. Yet, the same code is properly packaged on my colleague's machine (he uses m2e).
Does anyone have the same problem ?
Here is the most relevant part of the POM:
<packaging>war</packaging>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<!-- <version>2.3.2</version> -->
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</build>
It appears that the old code is still present somewhere; my guess would be your local repository. Try deleting any entry there that could contain the old code and then building your project again.
Check the <dependency> entry in pom.xml for that jar file. Change the <version> of that dependency to the latest one.

How can I switch the bytecode target level in IntelliJ using Maven 3 profiles

I am using maven profiles to switch between two "setups" in Intelli J. How can I switch the bytecode target level? That is: I want to change the following setting in compiler.xml
<bytecodeTargetLevel>
<module name="finmath-lib" target="1.6" />
</bytecodeTargetLevel>
Note: I tried to perform this via the following part in the respective Maven 3 profile
<profile>
<id>java-8</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>
but this does not work!
Note: The pom.xml etc. belongs to the finmath lib project, and if you are interested, it can be found at www.finmath.net
I am using IntelliJ IDEA 14.1.3 and it works with following config ...
<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>
</configuration>
</plugin>
Hope this is helpful. Thanks!
The problem is that you want to change it to 1.8.
I've tried to switch IntelliJ's compiler version between 1.7 and 1.6 or 1.5 using Maven profile successfully.
However when you want to change it to 1.8, the config will be ignored.
I don't know whether this is maven's problem or Intellj's.
It seems that the JDK 1.8 is not well supported now, which is understandable.
You can open the Maven Projects tool window in IntelliJ and select the profile you want to use from the list.
In IntelliJ 13, File > Project Structure, set the Project language level to 8.0.

Is there a way to make maven build class files with UTF-8 without using the external JAVA_TOOL_OPTIONS?

I don't want to be dependable on a external environment variable to force maven to build my classes with UTF-8. On Mac, I was getting all sorts of problems when building with maven. Only the option below solved the problem:
export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
mvn clean install
However I am distributing my project and it does NOT make sense to rely on the user to set this environment variable to build the project correctly.
Tried everything as described here: enabling UTF-8 encoding for clojure source files
Anyone has a light on that awesome Maven issue?
#Joop Eggen gave the right answer here: https://stackoverflow.com/a/10367745/962872
It is not enough to define that property. You MUST pass it inside the appropriate plugins. It won't go by magic inside there.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>...</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>...</version>
<configuration>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Yes there is, define
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
I was running into this problem, but only when running the compile from Emacs. I could not change the project's poms. What worked for me was to put the following in ~/.mavenrc
LANG=en_US.UTF-8

Categories

Resources