Sonar and MAVEN POM file with Dependencies - maven-3

I have setup Maven and Sonar to analyze .NET projects and it works fine for winforms projects. However when I add an MVC project it gives a build Failure because it can't find the System.Web.MVC.dll file. I have an Fx-Cop plugin that runs as part of Sonar.
One way to get around this would be to package the dll, copy local= true with the project. However I dont want to do that(any thing wrong with packaging the MVC file along?)
How do I add a dependency to the MVC dll? I followed this example and added it like this:
<dependencies>
<dependency>
<groupId>artifact_group_id</groupId>
<artifactId>System.WEb.MVC</artifactId>
<version>4.0.30319</version>
<type>library</type>
<scope>system</scope>
<systemPath>C:\DOTNET\DLLS\System.Web.Mvc.dll</systemPath>
</dependency>
</dependencies>
Still I get a Build Failure caused by FX-Cop. Looking at the FX-Cop logs I get a message as below:
The following error was encountered while reading module 'MyTestMvcApp': Assembly reference cannot be resolved: System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
I'm using apache-maven-3.0.2 and sonar-3.2. Can anyone help with this?
This is the full POM.XML file
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Test</groupId>
<artifactId>Sonar.For.NET.Test</artifactId>
<version>1.0</version>
<name>Testing </name>
<packaging>netpack</packaging>
<properties>
<visual.studio.solution>TestProjectsForSonarBuild.sln</visual.studio.solution>
<visual.test.project.pattern>*.Tests;*Test</visual.test.project.pattern>
<dotnet.tool.version>4.0</dotnet.tool.version>
<sonar.language>cs</sonar.language>
</properties>
<dependencies>
<dependency>
<groupId>artifact_group_id</groupId>
<artifactId>System.WEb.MVC</artifactId>
<version>4.0.30319</version>
<type>library</type>
<scope>system</scope>
<systemPath>C:\DOTNET\DLLS\System.Web.Mvc.dll</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.sonar-plugins.dotnet</groupId>
<artifactId>maven-dotnet-plugin</artifactId>
<version>0.6</version>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>sonar-maven-plugin</artifactId>
<configuration>
<toolVersion>3.5</toolVersion>
<buildConfigurations>Debug</buildConfigurations>
<rebuild>true</rebuild>
<language>cs</language>
</configuration>
</plugin>
</plugins>
</build>
</project>
Thanks for your time.

You can use the "sonar.fxcop.assemblyDependencyDirectories" property as described on the documentation page for the FxCop plugin.
So you have 2 solutions from there:
either your build process copies "System.Web.MVC.dll" in a temp folder, which you reference through the this property
or you directly reference an absolute path where this DLL can be found (but your property may then be not portable accros different environments/servers)

Related

Unable to find artifact from artifactory

I am using JFrog artifactory 3.2.1.1 with Maven 3.2.1.
I uploaded a built project that exists in libs-snapshot-local under the repository browser. If I browse to com.foo.project, I will see the project-1.0-20151113.133436-1.jar file and pom and metadata in the artifactory browser.
Even accessing http://example.com:8081/artifactory/webapp/browserepo.html?42&pathId=libs-snapshot-local:com/foo/project/1.0-SNAPSHOT/project-1.0-20151113.133436-1.jar shows me the jar file inside.
I used the settings.xml generator from the artifactory to generate the <repository> tag that I use in the following pom.xml:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.foo.bar</groupId>
<artifactId>exampleApp</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>exampleApp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<snapshots />
<id>snapshots</id>
<name>libs-snapshot</name>
<url>http://example.foo:8081/artifactory/libs-snapshot</url>
</repository>
</repositories>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>prepare-package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.foo</groupId>
<artifactId>project</artifactId>
<version>1.0</version>
<type>jar</type>
<includes>myFolder</includes>
<outputDirectory>${project.build.directory}/newFolder/js/gmoketest</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
I just get:
Failed to execute goal
org.apache.maven.plugins:maven-dependency-plugin:2.8:unpack (unpack)
on project exampleApp: Unable to find artifact. Failure to find
com.foo:project:jar:1.0 in
http://example.com:8081/artifactory/libs-snapshot was cached in the
local repository, resolution will not be reattempted until the update
interval of snapshots has elapsed or updates are forced
And if I change
http://example.foo:8081/artifactory/libs-snapshot
to
http://example.foo:8081/artifactory/libs-snapshot-local
Then I get:
[ERROR] Failed to execute goal
org.apache.maven.plugins:maven-dependency-plugin:2.8:unpack (unpack)
on project exampleApp: Unable to resolve artifact. Could not transfer
artifact com.foo:project:jar:1.0 from/to snapshots
(http://example.com:8081/artifactory/libs-snapshot-local): Failed to
transfer file:
http://example.com:8081/artifactory/libs-snapshot-local/com/foo/project/1.0/project-1.0.jar.
Return code is: 409 , ReasonPhrase:Conflict.
I will keep deploying new snapshots now and then to the same project-1.0 and would like this pom file to just include the latest SNAPSHOT build when building from the artifactory.
The answer was, as I quickly figured out that the version needs to be specified as:
<version>1.0-SNAPSHOT</version>

In multimodule maven project, how to execute proper goal for each module?

I have three maven projects in my prototype:
bookman (main project)
|-- bookman-back-lend (module, simple service app)
\-- bookman-front-web (module, simple web app)
This is very simple example book lending app (for library or something) to learn various technologies and all of that.
Problem is that I can't make parent pom to execute goals in module poms.
Calling mvn clean package wildfly:deploy in any module individually works without problem. It compiles, deploys, war is replaced, Wildfly 8 does its thing, etc. But if I call parent POM with mvn clean package (I'm not even sure what to call...), it doesn't do much - certainly it does not deploy to wildfly any of modules. Wildfly does not budge. In parent POM, calling mvn clean package wildfly:deploy does not work, of course.
Here is parent pom.xml:
<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>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Main BookMan superproject.</name>
<description>Main BookMan superproject.</description>
<modules>
<module>bookman-front-web</module>
<module>bookman-back-lend</module>
</modules>
</project>
And module POMs, with some stuff cut out for brevity (I assume they aren't related to my problem).
bookman-front-web/pom.xml:
<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>
<artifactId>bookman-front-web</artifactId>
<packaging>war</packaging>
<name>Frontend project - webpage.</name>
<description>Web page to handle lending books.</description>
<parent>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
...
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
...
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package wildfly:deploy</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn clean package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
And bookman-back-lend/pom.xml:
<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>
<artifactId>bookman-back-lend</artifactId>
<packaging>war</packaging>
<name>Backend project - lending.</name>
<description>Business logic to handle books, users and act of lending.</description>
<parent>
<groupId>org.mader.bookman</groupId>
<artifactId>bookman</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<properties>
...
</properties>
<repositories>
...
</repositories>
<dependencyManagement>
...
</dependencyManagement>
<dependencies>
...
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package wildfly:deploy</defaultGoal>
<plugins>
<plugin> <!-- To use, run: mvn clean package wildfly:deploy -->
<groupId>org.wildfly.plugins</groupId>
<artifactId>wildfly-maven-plugin</artifactId>
<version>${version.wildfly.maven.plugin}</version>
</plugin>
</plugins>
</build>
</project>
It should be possible to have customized goals to execute for each module individually, right? Right? Otherwise those aggregate POMs are rather useless.
Unfortunately, questions like this suggest that maven goal executed on parent POM is exactly same goal to execute on child POMs. Who thinks up something like that? What if I need completely different goals in each child module?
All answers I found are few years old, maybe... just maybe... sanity prevailed and maven now allows something like that? After all, all information neccessary to do this should be accessible, like defaultGoal tag.
Problem was solved in radical way. Maven is dead, long live Gradle.
I was tinkering with it last few days and I already moved prototype to point where it actually works. While there is still tons of work left, I already know I will not be looking back.
So, yeah... if you have requirements that cannot be done easily or at all with Maven for various reasons, then consider Gradle. In fact, consider Gradle anyway.

possible bug in Maven + plexus + eclipse compiler on case sensitive packages?

I encounter a very strange problem with Maven and Eclipse compiler.
While in Eclipse+m2eclipse, I have no problem compiling a small project (archetype quick start) with the following single class.
package test.test;
import com.Ostermiller.util.CSVParser;
public class TestCaseSensitive {
CSVParser csvParser;
}
Ostermiller utils is added to pom.xml. Eclipse Kepler compiles the project.
Next, mvn compile works out-of-the-box.
Now the issue, I switch to compiler 3.1 and asks for Eclipse compiler (to be able to handle same compilation issues in console mode as well as IDE mode). This is the POM :
<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>test</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.ostermiller</groupId>
<artifactId>utils</artifactId>
<version>1.07.00</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerId>eclipse</compilerId>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<fork>false</fork>
<compilerArgument>-err:nullAnnot,null</compilerArgument>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-compiler-eclipse</artifactId>
<version>2.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
And now here is the result :
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project test: Compilation failure: Compilation failure:
[ERROR] /home/me/workspaces/4/3/ws/test/src/main/java/test/test/TestCaseSensitive.java:[3] The import com.Ostermiller cannot be resolved
[ERROR] /home/me/workspaces/4/3/ws/test/src/main/java/test/test/TestCaseSensitive.java:[7] CSVParser cannot be resolved to a type
The package com.Ostermiller exists (it compiles in maven default compiler as well in Eclipse IDE), but not after switching to eclipse compiler.
Please note that the reported error path is also wrong :
[ERROR] /home/me/workspaces/4/3/ws/test/src/main/java/...
should be
[ERROR] /home/me/workspaces/4.3/ws/test/src/main/java/...
Has someone an idea? Where shall the potential bug be reported?
Have you tried using the jdt compiler provided by tycho?
See http://wiki.eclipse.org/Tycho/FAQ#Can_I_use_the_Tycho_compiler_support_in_non-OSGi_projects.2C_too.3F
That'd give you :
<plugin>
<!-- Use compiler plugin with tycho as the adapter to the JDT compiler. -->
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>jdt</compilerId>
<source>1.7</source>
<target>1.7</target>
<optimize>true</optimize>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<fork>false</fork>
<compilerArgument>-err:nullAnnot,null</compilerArgument>
</configuration>
<dependencies>
<!-- This dependency provides the implementation of compiler "jdt": -->
<dependency>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-jdt</artifactId>
<version>${tycho-version}</version>
</dependency>
</dependencies>
</plugin>
Currently tycho-version=0.18.0

unable to convert my maven to eclipse

Please, I am new to maven and trying to build my first maven project. So, here are a few things I did:
from my command line into a directory called MavenProject I created:
mvn archetype:generate
and then choose a number to apply number, I entered 15 then;
Choose com.dyuproject.protostuff.archetype:basic-webapp version:
I chose version 1.0.7
groupId: com.henry
artifactId: HibernateTest
and the rest, I just entered..
and the project was created but then I typed in mvn eclipse:eclipse, I got an error that there was no pom.xml file even though I can see there is one in my mavenProject. so, I changed into the HibernateTest directory and in that directory, I tried the mvn eclipse:eclipse command again but this time, it gave the eorror:
Plugin com.dyuproject.protostuff:protostuff-maven-plugin:1.0.2-SNAPSHOT or one of its
dependencies could not be resolved: Failed to read artifact descriptor for
com.dyuproject.protostuff:protostuff-maven-plugin:jar:1.0.2-SNAPSHOT: Could not find artifact
com.dyuproject.protostuff:protostuff-maven-plugin:pom:1.0.2-SNAPSHOT
I tried to solve this by going to mvnrepository.com and found the protostuff maven and added the dependencies but still couldn't solve it. here is my pom.xml file:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLS$
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_$
<parent>
<artifactId>Hibernate</artifactId>
<groupId>com.henry</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.henry</groupId>
<artifactId>Hibernate-model</artifactId>
<name>Hibernate :: model</name>
<packaging>jar</packaging>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-maven-plugin</artifactId>
<version>${protostuff.version}</version>
<configuration>
<protoModules>
<protoModule>
<source>src/main/resources/com/henry/model/model.proto</source>
<outputDir>src/main/java</outputDir>
<output>java_bean</output>
<encoding>UTF-8</encoding>
<options>
<property>
<name>generate_field_map</name>
</property>
<property>
<name>separate_schema</name>
</property>
<property>
<name>builder_pattern</name>
</property>
<property>
<name>generate_helper_methods</name>
</property>
</options>
</protoModule>
</protoModules>
</configuration>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-maven-plugin</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-codegen</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-maven-plugin</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-codegen</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-compiler</artifactId>
<version>1.0.7</version>
</dependency>
</dependencies>
</project>
Any help will be appreciated. Sorry am a newbie :)
First try to build the project on the command line: mvn install. That should succeed.
But it looks like it won't, as it is looking for a SNAPSHOT dependency version of ${protostuff.version}. When I look in the Maven central repository, there's really only release versions (as it should be). Somehow you have to fix that, probably in your parent project.
The POM file should be in the root of the project and called pom.xml. Where you find the POM file called pom.xml is the root of the project :)
Only use eclipse:eclipse as a last resort. If you have free choice of Eclipse, and you have no wizardry in your Maven projects, you should be able to use the m2e (m2eclipse) Eclipse plugin.
That's an Eclipse plugin, not a Maven plugin, so no additional steps are needed on the command line. Just go into Eclipse, make sure the m2e plugin is installed. Then import your project (Import, "Existing Maven Projects").
All the files that Eclipse needs for its own bookkeeping should be created upon import and be based on the POM file. I.e. .classpath, .project and .settings (folder). If you have any of those prior to importing into Eclipse, you may be better off removing them (they may be remnants of your eclipse:eclipse attempts.

Create pom.xml file

Could anayone give me some sugestions on how to create a pom.xml file for a multimodules project, that is build with ant? I need to create this pom.xml file in order to analyze the project with Sonar.
I suggest to follow the instructions from the Sonar documentation. See Analyzing Java Projects:
Project with multiple sources directories
If your non-maven project contains
more than one sources directory, you
can specify which sources directories
to analyse by adding a new section
about the Build Helper Maven Plugin
into your pom.xml file :
<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>[YOUR.ORGANIZATION]</groupId>
<artifactId>[YOUR.PROJECT]</artifactId>
<name>[YOUR PROJECT NAME]</name>
<version>[YOUR PROJECT VERSION]</version>
<build>
<sourceDirectory>[YOUR SOURCE DIRECTORY]</sourceDirectory>
<outputDirectory>[YOUR CLASSES/BIN DIRECTORY</outputDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
<excludes>
<exclude>**/*.*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>[YOUR SOURCE DIRECTORY 2]</source>
<source>[YOUR SOURCE DIRECTORY 3]</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<properties>
<sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
<sonar.phase>generate-sources</sonar.phase>
</properties>
</project>
Replace the parameters :
...
And execute the maven2 plugin as explained in the installation guide :
mvn sonar:sonar
There is now a Sonar Ant Task that you can use, or there is also the Sonar Runner
What you put in the pom.xml is going to depend what dependencies you need to use and what plugins you need to run. Check out the Intro to POM to see what it is made up of.
I think you can try to use the builder-helper-maven-plugin, currently, latest version is 1.5.
as documented http://docs.codehaus.org/display/SONAR/Analyzing+Java+Projects. However, just change the plugin version to 1.5 and use mvn sonar3:sonar. Most importantly, dont forget <sonar.phase>generate-sources</sonar.phase>, without this, it doesn't work.
as for the output directory, if using eclipse, you can specify the output directory for each module, and make them point to the same folder. Use this folder as the outputdirectory for pom.xml. remember to disable scrub, if using eclipse.

Resources