Running Maven PMD locally using Sonar ruleset - java

I'm attempting to run pmd locally using Maven. In project I run
mvn jxr:jxr pmd:pmd
but receive error :
net.sourceforge.pmd.RuleSetNotFoundException: Can't find resource 'rulesets/basic.xml' for rule 'UnusedNullCheckInEquals'. Make sure the resource is a valid file or URL and is on the CLASSPATH
Here is a snippet of the rules file which was exported :
<?xml version="1.0" encoding="UTF-8"?>
<ruleset>
<rule ref="rulesets/basic.xml/UnusedNullCheckInEquals">
<priority>3</priority>
</rule>
</ruleset>
I've updated pom file to reference custom ruleset :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.2</version>
<configuration>
<linkXref>true</linkXref>
<rulesets>
<ruleset>c:\\pmd\\export.xml</ruleset>
</rulesets>
</configuration>
</plugin>
To export the ruleset so can run pmd locally using Maven is it required to export this file (basic.xml) & all other xml files which together comprise the "Sonar Way" quality profile ? If so where can these files be accessed on Sonar ?

You can access thru "Quality Profiles > Sonar Way > Permalinks > PMD", although, I think "Sonar Way" pmd rules are empty by default (all rules disabled).
Also, have in mind that version 3.2 of maven-pmd-plugin looks for rules not in rulesets but in rulesets/java, so you might have to replace that path in the exported file (at least the file that you get with Sonar 4.2).
Finally, I would recommend not using a hardcoded path to reference the ruleset file, use something like:
<ruleset>${project.basedir}/rules/pmd-rules.xml</ruleset>
Hope this helps!

Related

How can I add jar file to project instead of adding to Tomcat lib folder?

Via this github project, I store Tomcat sessions in Redis:
https://github.com/chexagon/redis-session-manager
And in src/webapp/META-INF/context.xml file I added new commands:
<Manager className="com.crimsonhexagon.rsm.redisson.SingleServerSessionManager"
endpoint="redis://localhost:6379"
sessionKeyPrefix="_ib_"
saveOnChange="false"
forceSaveAfterRequest="false"
dirtyOnMutation="false"
ignorePattern=".*\\.(ico|png|gif|jpg|jpeg|swf|css|js)$"
maxSessionAttributeSize="-1"
maxSessionSize="-1"
allowOversizedSessions="false"
connectionPoolSize="100"
database="0"
timeout="60000"
pingTimeout="1000"
retryAttempts="20"
retryInterval="1000"
/>
If jar file is in Tomcat lib folder, then everything is fine. But in production if I add jar to lib folder it causes errors. Is there any other way for solving this problem? Can I change context.xml in such way that when I will deploy project, manager className will be able found relevant class?
P.S. There are some examples but I couldn't catch the sense:
Adding external resources to class-path in Tomcat 8
If someone can enlighten me, I will be grateful.
Thanks in advance
I could find solving. There is a plugin in maven for deploying war file. With this plugin it is possible to add files to war file. For example, I created folder which is named redis and add into it jar file. Then I added plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>redis</directory>
<targetPath>WEB-INF/lib</targetPath>
</resource>
</webResources>
</configuration>
</plugin>

Composite components in an external JAR are not recognized in Netbeans

I have packaged a number of composite components in a JAR. However, when using them in another project (using Maven), Netbeans editor puts red error lines under lines which use the composite component, even though the project compiles and runs as expected.
The folder structure for the composite component JAR look like:
compositeComponent.jar
META-INF
faces-config.xml
highcharts-taglib.xml
MANIFEST.MF
web.xml
maven
// maven stuff.
resources
highcharts
Chart.xhtml
Series.xhtml
Tooltip.xml
nz
co
kevindoran
highcharts
example
NZPopulationTrend.class
The highcharts.taglib.xml looks like:
<facelet-taglib version="2.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd">
<namespace>http://nz.co.kevindoran/highcharts-jsf</namespace>
<composite-library-name>highcharts</composite-library-name>
</facelet-taglib>
[Side note: The faces-config.xml and web.xml are present to allow the 'JAR' to be deployed as a WAR by changing the file extension to WAR (this is to done to run the examples).]
In my current project, I have specify a Maven dependency on the above project like so:
<dependency>
<groupId>nz.co.kevindoran</groupId>
<artifactId>jsf-menu</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
In a JSF page, I use on of the composite components like so:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:hc="http://nz.co.kevindoran/highcharts-jsf">
....
<hc:TimeChart title="Price Over Time" xLabel="Date" yLabel="Sold Price (NZD)">
<hc:TimeSeries name="Sold" series="#{cc.attrs.model.priceVsTimeChart.soldSeries}"/>
</hc:TimeChart>
....
</html>
Red error lines appear under all lines above, with message: "No library found for namespace http://nz.co.kevindoran/highcharts-jsf"
How do I get these error lines to be removed? I have seen many Netbeans bug reports for similar issues, but all seem resolved.
This error occurs on Netbeans 7.1, 7.2 and 7.3 (including 7.3.1).
I have absolutely the same problem. In my case it depends on the /src/main/java folder. If it's exist (only in the project and not even in the jar) the project which includes this library shows the "No library found for namespace... "
When i remove the "java" folder it works. But then my backing bean class is missed in the jar...
Tried with Netbeans 7.2 and 7.3, maven 2
Solution:
Generate a second project which contains the Java source files. (called: jsf-lib-java)
In jsf-lib project (your composite component project with xhtml) delete the "java" folder and all *.java sources.
add in the jsf-lib pom.xml following configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.mycompany.project</groupId>
<artifactId>jsf-lib-java</artifactId>
<version>1.0-SNAPSHOT</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/</outputDirectory>
<includes>**/*.class</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
That's it. This will generate a "good" jar file with the required *.class files. So it's possible to "trick" Netbeans.
Now i work with this solution. It's a hack but didn't found a better solution.

How to use Maven and various application.properties for artifact generation of different testing regions in a Spring configured app

I want to use Maven to handle artifact generation for the different local and testing regions. I believe I can use different profiles but I am not certain.
In Maven can I select different directories to select files used on packaging (such as application.properties)? How would I set that up?
An idea of what I want is to have a the following folders for resources in my project
local
build server
dev
sys
prod
Each folder should contain a different version of application.resources which is a file in Spring that can be used to handle hard-coded strings for use in variables. For local builds- our developers also work on different operating systems. Should I require I want to make it seamless on different OS' also.
Key outcomes would be:
Control Maven lifecycle phases from inside the IDE (IntelliJ)
Not complicate phases and team processes
Keep things as consistent for each developer
Make the different configurations per developer/region appear invisible when running a phase e.g. install
Ideally I would have my project set up according to best practices (Duvall, Matyas, Glover).
We provide different properties currently but not by way of different folders. We do this
via a mix of
Spring's PropertyPlaceholderConfigurer
Maven profiles (something we use to build our Dev environment),
Build Server (TeamCity in our case)
Maven phases to produce the correct artifact
start-up and build arguments
My understanding of what we do is limited, but hopefully this serves as a useful example for others and maybe myself to consider.
We provide parameters, as you'll see below, to point to different property files.
Each property file has configuration for a region/environment. I'll explain the current use
as best I can in-case it provides some use to others.
To use Maven profiles we have created a profile in our pom identified as development which includes a region configuration property called env. I don't yet know entirely how that is being used yet in our project however you'll see below our POM includes the a Maven Compiler plugin and a Maven Tomcat plugin.
Day to day, as developers we run our our applications locally on Tomcat from within IntelliJ
and provide the env property. On start-up the env property is provided as an argument to
set to classpath*:dev-common.properties.
This file is a properties configuration file - setting placeholder values for our different
regions.
The value of env is made available to our PropertyPlaceholderConfigurer
Example 1 - Implementation of Maven profile in pom.xml:
The implementation of a profile in our pom is:
<profile>
<id>development</id>
<activation>
<property>
<name>env</name>
<value>development</value>
</property>
</activation>
<build>
<pluginManagement>
<plugins>
<plugin>
<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.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0-SNAPSHOT</version>
...
</plugin>
</plugins>
</pluginManagement>
</build>
</profile>
Example 2 - Property placeholder configurer for normal build:
We also make use ofa Spring component, a PropertyPlaceholderConfigurer. We use this in collaboration with a build argument to set up a classpath pointer to resource files.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>
${env}
</value>
</list>
</property>
Example 3 - Property placeholder configurer for test:
We have Spring Contexts specifically set up for integration testing which also use the PropertyPlaceholderConfigurer. These are picked up by a integration testing class using a combination of #ContextConfiguration(locations = {"classpath:test-dataexchange-application-context.xml"}) and #RunWith(SpringJUnit4ClassRunner.class)).
In the testing context we configure the PropertyPlaceholderConfigurer as follows to pick up the properties of an integration testing region:
<bean id="testpropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:dev-local-common.properties</value>
</list>
</property>
Other notes:
We make use of Team City for build management but I have not seen how these settings are
used there, if they are at all. I can conceive there's an ability to combine the above approaches together to aid Continuous Integration and Delivery.
I do not see where the profile identified as development is being used. It is something I
must follow up with my fellow team members.
Resources:
Building for different environments at the Maven Project site.
Maven 3 does not allow configuration of a profile outside of a pom or settings.xml (the Maven configuration file) and says that users who used these external settings should now put them inside of settings.xml
If you are using Spring boot, there is an easy way of doing this.
Create two profiles in maven, and set a property in each profile with the name of the Spring profile you want to execute.
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<!-- Default spring profile to use -->
<spring.profiles.active>dev</spring.profiles.active>
<!-- Default environment -->
<environment>develop</environment>
</properties>
</profile>
Inside your application.properties, add this property:
spring.profiles.active=${spring.profiles.active}
Create an application.property for each profile, using this pattern application-profile.properties. For example:
application-dev.properties
application-prod.properties
Be sure to active filtering in the resource plugin:
...
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
...
Another way is to create a file during the execution of maven called activeprofile.properties. Spring boot looks this file to load the active profile. You can create this file as follows:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<phase>prepare-package</phase>
<configuration>
<target>
<echo message="spring.profiles.active=${spring.profiles.active}" file="target/classes/config/activeprofile.properties" />
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
</configuration>
</plugin>
Aim to generate an artifact for each environment at one time on the central server (CI/Build server), aim to generate an artifact and start/test the application with one click locally, provide a consistent easy to learn way to check out and run your build, and check in and configure your CI.
You can use profiles in Maven and utilize Maven targets to achieve the right build using a build server which in our case is TeamCity.
Use property placeholder configurer in Spring context with an application.resources file for each region and a filename mask e.g. application-resources-${region}.

How does m2e decide if project has java nature?

I'm using a special Maven plugin which provides a custom packaging type and defines a special lifecycle mapping. But it is still a Java-like project so I'd like Eclipse (with m2e plugin) to import the project as a Java project (With java nature in .project file). But it always imports it as a simple project without java nature. I always have to add the java nature to the .project file manually.
I also tried keeping packaging type "jar" and changed the plugin to provide a custom lifecycle mapping for "jar". But still m2e doesn't recognize it as a java project.
So how does m2e decide if it must enable the Java nature in the project or not? Or is there some special m2e configuration I can put into the Maven pom.xml to tell m2e that my project is a java project?
Found it! No need to change the packaging type to "JAR". I only need to include the maven-compiler-plugin in the lifecycle mapping:
<!DOCTYPE component-set>
<component-set>
<components>
<component>
<role>org.apache.maven.lifecycle.mapping.LifecycleMapping</role>
<role-hint>custom</role-hint>
<implementation>org.apache.maven.lifecycle.mapping.DefaultLifecycleMapping</implementation>
<configuration>
<phases>
<compile>
org.apache.maven.plugins:maven-compiler-plugin:compile,
my.custom.plugin:compile
</compile>
</phases>
</configuration>
</component>
</components>
</component-set>
Now m2e adds the java nature to the project when importing it and still executes the custom life cycle.
try adding maven eclipse plugin to your POM file.
> <plugin>
> <groupId>org.apache.maven.plugins</groupId>
> <artifactId>maven-eclipse-plugin</artifactId>
> <version>2.9</version>
> </plugin>

Howto pass Java system properties to maven-cargo container

I'm preparing a maven2 web project for continuous integration. I use the maven cargo plugin to automatically deploy the WAR to Tomcat6x before running integration tests.
My code depends on some system properties which are set with MAVEN_OPTS=-Dfoo=bar. Unfortunately these properties are missing when the application is deployed to Tomcat:
System.getProperty("foo"); // null, when deployed to container by maven-cargo
How can I pass these properties to Tomcat?
You should be able to do this by using the systemProperties tag in the container definition of the plugin:
<container>
[...]
<systemProperties>
<MAVEN_OPTS>-Dfoo=bar</MAVEN_OPTS>
</systemProperties>
</container>
Or you can set this in a setenv.sh (on linux) file in your $CATALINA_HOME/bin/ directory. If this file does not exist you should create it and add the following line:
MAVEN_OPTS=-Dfoo=bar
Hope this helps.
You should be able to do this by using the systemProperties tag in the container definition of the plugin:
<container>
<systemProperties>
<foo>bar</foo>
</systemProperties>
</container>
this is equivalent to pass -Dfoo=bar in command line or in another option.

Categories

Resources