I'm using Spring/Roo with maven for an app server, and need to be able to post some special characters, like "ñ á é". When I print these characters on my server, and display them in console, they appear as "?", like this
System.out.print("ñ á é");
console print -> ? ? ?
How can they be properly encoded, i have another project without maven and spring-roo, using the same JVM, and i don't have this issues
hope someone can help thx in advance
I assume that in Roo the following parts is somewhere found in the pom.xml:
<project>
...
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
</project>
Related
Pls are there any Java libraries that can retrieve Maven dependencies from a POM file? Anything that does not require retrieving out put of a mvn command. Thanks
Well, you should parse the pom.xml with any xml parser of your choice.
Then construct the link to maven central repository by the following algorithm:
1. Each package in group id is translated to a folder:
Example:
<groupId>org.foo.bar</groupId> ==> org/foo/bar
Artifact name is also a folder and append it to the group id:
Example:
<artifactId>some-artifact</artifactId> ==> org/foo/bar/some-artifact
Version also becomes folder:
Example:
<version>1.2.3</version> ==> org/foo/bar/some-artifact/1.2.3
Now construct the jar name as "articatId-version.jar" and append it to the link.
Prepend the repository and you'll get a full-working path.
Here is a real working example:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
Gets translated to:
https://repo1.maven.org/maven2/org/springframework/boot/spring-boot/2.2.4.RELEASE/spring-boot-2.2.4.RELEASE.jar
As an alternative if you want some library that can work with dependencies don't want to call maven, take a look at Apache Ivy
Thanks.
I used the MavenProject library. Pretty straight forward.
I also used MavenXppReader to get the model that I passed to MavenProject(). The rest was a matter of calling the right methods.
When I run below code with two different project I get different outputs.
String myString = "Türkçe Karakter Testi : ğüşiöçĞÜİŞÇÖĞ";
String value = new String(myString.getBytes("UTF-8"));
System.out.println(value);
First project is non-maven java application created in Netbeans 8.2. And it gives me following result which i expect.
"Türkçe Karakter Testi : ğüşiöçĞÜİŞÇÖĞ"
And second project is maven java application project which is created in same way with following pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
This project gives me:
"Türkçe Karakter Testi : ğüşiöçÄ?ÜİÅ?ÇÖÄ?"
I checked both file with notepad++ and both of them are encoded with UTF-8
You're missing the encoding from your new String() constructor, so it's using the default encoding of your platform which isn't UTF-8 (looks like some variant of ISO-8859-1).
If you use the following code (which doesn't make much sense, but shows the default encoding botching things), you'll see that it's printed properly everywhere.
String myString = "Türkçe Karakter Testi : ğüşiöçĞÜİŞÇÖĞ";
String value = new String(myString.getBytes("UTF-8"), "UTF-8");
System.out.println(value);
What's the lesson here? Always specify the encoding to use when dealing with byte/character conversion! This includes such methods as String.getBytes(), new String() and new InputStreamReader().
This is just one of the many ways that character encoding can bite you in the behind. It may seem like a simple problem, but it catches unsuspecting developers all the time.
I also often faced with the same problems.
Configuring Maven Character Encoding
The problem
Run my code in IDE (idea/eclipse). All correct. Output had correct encoding and in the console and in output files.
Run my app after built Maven. When I try to run my App (jar) built with help maven mvn clean install
I got incorrect values in output related to incorrect encoding.
In the console and in output files which were generated in my app I saw incorrect and unexpected symbols
Warning in your console. This warning means that you have not set any character encoding for your project/environment.
Let's solve this problem. There are a couple of options you can consider.
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
Configuring Maven Character Encoding
1. Properties
A most popular and common way to set Maven Character Encoding is to use properties. These properties are supported by most plugins. These properties are easy to add. Just add them as a child element of the project element.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
[...]
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
[...]
</project>
2. Maven Resources Plugin
You can also specify Maven Character Encoding using the maven resources plugin.
The only drawback is that you have to include this plugin to your Maven pom.xml file.
JUST ADD THIS PLUGIN - It`s always helped me ))
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
[...]
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
[...]
</project>
3. Commandline
If you cannot alter the source code of a maven project, or you need to specify maven character encoding on a built server like Jenkins, Hudson, or Bamboo you can also add the encoding through the command line.
mvn -Dproject.build.sourceEncoding=UTF-8 -Dproject.reporting.outputEncoding=UTF-8 clean deploy
4. Maven Options
If you do a lot of small projects for personal gain you can also set this property globally in MAVEN_OPTS. The only drawback is that if you share your code base with another developer then the developer also has to add these MAVEN_OPTS. That’s why I do not recommend it.
set MAVEN_OPTS= -Dfile.encoding="UTF-8"
#See How to Configure Maven Character Encoding
Say, there is a property customProp in the pom.xml in my spring project.
2.4.snap
When I run my project, I can update its value like below
mvn clean install -DcustomProp=newValue
It's working well. It updates the value of customProp with newValue.
But I want to concat newValue with the previous value of customProp. So that the value of customProp will be 2.4.snapnewValue.
How can I do that?
Further, is it possible to replace snap with newValue so that the value of customProp will be 2.4.newValue.
<properties>
<customProp>snap</customProp>
</properties>
<version>2.4.${customProp}</version>
Would this be possible in your situation? Of course, use the correct tag where I'm using <version>.
The best way to approach this might be more like:
<properties>
<customPropPrefix>2.4.</customPropPrefix>
<customProp>snap</customProp>
</properties>
<version>${customPropPrefix}${customProp}</version>
Now you can specify both -DcustomPropPrefix=2.4. and -DcustomProp=newValue.
Rather than build and clean your maven project forcefully it will sure update your maven projectenter image description here
Is there any way to disable certain metrics from selected packages in Sonar? I use Sonar to analyze my project and in Entity and DTO packages I have some code that is equal - the same field ID with annotations, etc is being reported as a duplication by Sonar. It has absolutely no sense to me so I'd like to disable it. How can I achieve this? Using the global exclusions option disables all metrics on selected package but how to do it just for code duplications?
With a newer SonarQube installation, you can use sonar.cpd.exclusions to exclude certain files only from duplicate checks.
See: https://docs.sonarqube.org/latest/analysis/analysis-parameters/
Example:
sonar.cpd.exclusions=**/AssemblyInfo.cs,**/*.g.cs,**/Mappings/*.cs
You can exclude resources using the standard "sonar.exclusions" parameter or use the Switch Off violation plugin to exclude "Duplicated code" violations.
Note that the 2nd option (use of the switch off plugin) works only if you're using the SQALE plugin, which embeds the "sqale-java:DuplicatedBlocksCheck" rule.
For me works its:
<sonar.cpd.exclusions>
com.simulate.java.dto\**
<\sonar.cpd.exclusions>
I have mult modules java projects just like that:
- parent
-- project-a
-- project-b
-- project-c
in the pom.xml of parent project inside of tag <properties> i put:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<sonar.cpd.exclusions>
com.simulate.java.vo\**,
com.simulate.java.dto\**
<\sonar.cpd.exclusions>
<\properties>
Just like that. I hope I helped you.
You can add these files to the properties in your pom.xml:
This one is to exclude from code coverage:
<sonar.coverage.exclusions>
your file paths
</sonar.coverage.exclusions>
This one is to exclude from code duplication:
<sonar.cpd.exclusions>
your file paths
</sonar.cpd.exclusions>
I would really like to make maven write the "target" folder to a different device (ramdisk), which I would normally consider to be a different path. Is there any maven2-compliant way to do this ?
I am trying to solve this problem on windows, and a maven-compliant strategy would be preferred.
If you happen to have all of your projects extending a corporate parent pom, then you could try adding Yet Another Layer of Indirection as follows:
Corporate POM:
<build>
<directory>${my.build.directory}</directory>
</build>
<properties>
<!-- sensible default -->
<my.build.directory>target</my.build.directory>
</properties>
In your settings.xml:
<properties>
<!-- Personal overridden value, perhaps profile-specific -->
<my.build.directory>/mnt/other/device/${project.groupId}-${project.artifactId}/target</my.build.directory>
</properties>
If the local POM definition takes precedence over the settings.xml definition, then you could try omitting the default value at the cost of having every Maven instance in your control (developers, build machines, etc) specify ${my.build.directory} in its settings.xml.
Actually, Maven is not as constrained as everybody thinks, all the POMs are extended of one Super POM in which is defined the name of the target folder
<build>
<directory>target</directory>
<outputDirectory>target/classes</outputDirectory>
<finalName>${artifactId}-${version}</finalName>
<testOutputDirectory>target/test-classes</testOutputDirectory>
.
.
.
</build>
Of course you can overwrite with any value you want, so just go ahead and change the <directory /> element (and other related elements) in your POM
just in case if you want to fix this for your own Maven3 and not touch anything in the project, locate file:
$MAVEN_HOME/lib/maven-model-builder-3.X.Y.jar
and update super-pom inside
org/apache/maven/model/pom-4.0.0.xml
changing line
<directory>${project.basedir}/target</directory>
in
<directory>/tmp/maven2/${project.groupId}-${project.artifactId}/target</directory>
so next time when you will build any maven project - it will put all classes under /tmp/