Sonar: non-maven project -> exclude a directory in src - maven

I have a non-maven project, but I have to use Sonar to make a code analysis once. So I created a pom.xml which works great. I see the analysis of all files and folders below my src directoy using this pom 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>my.group.id</groupId>
<artifactId>arifactId</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>MyApplication</name>
<build>
<sourceDirectory>src</sourceDirectory>
</build>
<properties>
<sonar.dynamicAnalysis>false</sonar.dynamicAnalysis>
</properties>
</project>
But I want to exclude one directory and all subdirectories of it. I have the two directories src/fr/ and src/com/. I only want to have src/fr/ and exclude src/com/.
Changing
<sourceDirectory>src</sourceDirectory>
to
<sourceDirectory>src/fr</sourceDirectory>
I get this error:
Failed to execute goal org.codehaus.mojo:sonar-maven-plugin:2.0:sonar
(default-cli) on project arifactId: Can not execute Sonar: Sonar is
unable to analyze file : '/Users/tim/workspace/src/fr/xxx/dao/TestClass.java':
The source directory does not correspond to the package declaration fr.xxx.dao

Normally, the The source directory does not correspond to the package declaration, as it told us, is an error when the package declared at the java file does not correspond with the directory structure e.g.
package my.test.java;
public class MyTest {}
The directory should be my/test/java/MyTest.java,
please note the src is treated as a sourceDirectory.
In your case you have changed the sourceDirectory from src to src/fr that means
package fr.xxx.dao;
public class TestClass{}
The directory is xxx/dao/TestClass.java,
please note the src/fr is treated as a sourceDirectory. Then the fr is ignored.
Normally when we would like to exclude somes package from the Sonar analysis, it can be simply done by setting them at each quality maven plugin e.g. findbugs,PMD, cobertura, etc.
Anyhow the Sonar also provides the configuration for each project as well. We can set by using the following steps: -
Please note: I'm using Sonar version 3.5, the menu may be different if you're using the different version.
Go to our sonar web site, e.g. https://myhost/sonar
Go to our project by selecting it from the dashboard.
At the top right you will see the Configuration menu. Click it and select Settings.
At the Settings page, select Exclusions menu.
At Exclusions you can simply set the excluded modules which is able to use the wildcard as well. (You may see the example at the bottom of the page.)
The example from your case, the exclusion should be fr/**. (Exclude all under the folder fr).
Please refer to the following link: -
Project Administration
Excluding Files and
Analysis Parameters
I hope this may help.

Related

How to compile specific dependencies using maven

I have a situation where in i need to clean and install couple of dependencies of my maven project. While I am working on this project i am making changes in these dependencies and have to manually clean and install for every small change i am making. I am trying to find a maven command which will make my life easy.
project-bpm-process <-- parent project
project-odata-service - < dependency >
project-core-service - < dependency >
I cannot put them as sub modules as they are not really modules of my this project, they are simply dependencies. So, literally group-id does not match in complete sense (there is a partial match but does not help in any way).
Update 1:
Tried the option 2 suggested by Mark. I see below error which indicates that the sub modules (aggregated projects) are not found under the parent project's folder.
[ERROR] [ERROR] Some problems were encountered while processing the
POMs: [ERROR] Child module
E:\STS-Workspaces\default-workspace\project-bpm-process-artificial\project-core-service
of
E:\STS-Workspaces\default-workspace\project-bpm-process-artificial\pom.xml
does not exist #
[ERROR] Child module
E:\STS-Workspaces\default-workspace\project-bpm-process-artificial\project-odata-service
of
E:\STS-Workspaces\default-workspace\project-bpm-process-artificial\pom.xml
does not exist #
[ERROR] Child module
E:\STS-Workspaces\default-workspace\project-bpm-process-artificial\project-bpm-process
of
E:\STS-Workspaces\default-workspace\project-bpm-process-artificial\pom.xml
does not exist #
I just created a new maven project with packaging "pom" type and added other projects as modules. Now, "project-bpm-process-artificial" has become artificial parent of all the three projects I was talking about.
From maven documentation, i see that the path is relative.
Update 2:
Location of actual pom is located at: *E:\STS-Workspaces\default-workspace\project-bpm-process-artificial*
But other referenced projects are in *C:\Users\ramgo\git* and *E:\git-repos*. These projects are imported into eclipse for development.
The pom.xml is here:
<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>project-bpm-process-artificial</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>project-bpm-process-artificial</name>
<url>http://maven.apache.org</url>
<modules>
<module>project-core-service</module>
<module>project-odata-service</module>
<module>project-bpm-process</module>
</modules>
<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>
</dependencies>
</project>
Solution: For all practical reasons I found option 1 is easy to implement (option 1 provided by Marks). It hardly took 5 minutes to write a batch script. Here is the one for handy reference.
set core=<directory_path_of_core_project>
set module_one=<<directory_path_of_module_one>>
set module_two=<<directory_path_of_module_two>>
cd %core%
call mvn clean install
cd %module_one%
call mvn clean install
cd %module_two%
call mvn clean install
Option 2 seems interesting but not feasible in my case. Links don't work and no way to refer absolute path for sub modules.
.....
The notion of Maven dependency assumes that the artifact is build externally (not even necessarily with maven) and available to your project as a third-party jar.
So, the terminology in the question is misleading to me.
If you have some third-parties (I assume having their own pom.xml) but are external to your project, then obviously in your project you can't manage them. Maven can't build external stuff.
So, based on these assumptions, the choices are:
Option 1
Create a script that will:
enter the dependency directory
run mvn install on that directory
enter your project's directory
run mvn whatever on your project
Option 2
Create an "artificial" pom.xml that will have packaging type "pom" and will list both your project and the dependencies as submodules (your project and dependencies will be peers):
|__some_folder
|__pom.xml
|__dependency1
| |__pom.xml
|__dependency2
| |__pom.xml
|__your-project
|__pom.xml
In this case you will be able to operate with both your project sources like one project and you'll be able to use the following:
cd some_folder
mvn clean install --projects <your-project> --also-make
So that if your project has dependencies in other modules, they'll also be built
I would probably go with the second option, but its your choice really
Update 1
Based on the information you've provided:
Don't really count on eclipse, its not really relevant at this point. You should try to get to the point where running maven from command line should work. The eclipse will follow your poms once you've done everything right.
If you place the "artificial" pom into E:\STS-Workspaces\default-workspace\project-bpm-process-artificial then all the modules should be in sub-folders:
E:\
|_ STS-Workspaces
|_ default-workspace
|_ project-bpm-processes-artificial
|_ project-core-service
| |_pom.xml of that module
| |_.git // it can be a root of git repo
| |_src
|_ project-odata-service
| ...
|_ project-bpm-process
After that you can do the following to check yourself:
cd E:\STS-Workspaces\default-workspace\project-bpm-process-artificial
mvn clean install
It should compile all the libraries and your project
Then if you want to build your project (assuming its called project-bpm-process) then you can do from the same folder:
mvn clean install --projects project-bpm-process --also-make
If it has a dependency on project-core-service but, say, not on project-data-service then only the project-core-service will be rebuilt
Now when the maven if sorted out, you can add eclipse workspace in any other folder. I can't comment much on eclipse since I'm an IntelliJ user. In intelliJ you can just import this artificial pom and it will automatically recognize all the projects. In eclipse I think it should work similarly

maven fails to recognize jetty is installed

fresh meat newbie on GCP / Maven on
OSX 10.14.3 with Visual Studio Code (latest)
GCP SpringBoot API with Maven
other questions on jetty seem to be further along than me.
the 'flow' below is to reveal steps to get to my question in the title...I think it's important to see how I got to where I am, and if you are so kind to offer help, you would want to know this? ok, here we go...
I downloaded the GCP getting-started-java github example and want to run the bookshelf example.
When I look at the multiple POM files I see that each references a project ID for GCP.
I can't use the same project ID as they are unique, just like GCP bucket names.
So, when I run
gcloud init
and select or create a configuration and make my own project with a unique project id, does that automatically override every POM file definition of project ID? Or do I need to do some maven clean command to change it???
Well... when I RTFM in each folder, it says to
mvn clean jetty:run-exploded -Dbookshelf.bucket=MY-BUCKET
heck even tried:
mvn jetty:run
and I get a build failure that says:
[ERROR] No plugin found for prefix 'jetty' in the current project and in the plugin groups
so... I
brew install jetty
Then to 'get started' jetty says I have to copy the 'plug in' details into my POM file... which one, as there are several??
But when I installed the VS Code plugin, it already updated all POM files; I still get the "No plugin found for prefix 'jetty'" error
I guess I'll stop with that question:
how do I get maven to 'know' that jetty is installed and work with it?
When you use the shorthand plugin goal jetty:run-exploded or jetty:run maven is attempting to find the plugin. This shorthand form will need to resolve the groupId:artifactId:version:goal in order to run.
The long-hand form of that would be ...
$ mvn org.eclipse.jetty:jetty-maven-plugin:9.4.15.v20190215:run
To fix this, just add the plugin to your 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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<build>
...
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.15.v20190215</version>
</plugin>
</plugins>
</pluginManagement>
...
</build>
</project>
The above will always use that specific version of jetty-maven-plugin when you use the shorthand syntax.
Alternatively, and with less control over which version to use, is to setup a pluginGroup in maven's $HOME/.m2/settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
...
<pluginGroups>
<pluginGroup>org.eclipse.jetty</pluginGroup>
</pluginGroups>
...
</settings>

maven versions:set not updating parent pom

I've following parent pom 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.xyz</groupId>
<artifactId>abc</artifactId>
<version>1.0.0.36</version>
<packaging>pom</packaging>
<name>Common Data Access Framework</name>
<build>
<pluginManagement>
...
...
Now when I run following command from the same folder where I have this parent pom file:
mvn versions:set -DnewVersion=9.9.9.9
It will update the version details in all child modules and pom file "except" it's own (the parent). This is really strange as I'm running it from the very same folder.
I have hit a similar problem yesterday. The cause was that my parent pom.xml had leading whitespace before the xml tag. After removing it version:set worked as expected, setting the version across all poms,parent and children.
I don't know if this was the solution in your case but I'm posting this to save another poor soul hours of stupid debugging.
Tried the following here with Maven 3.3.3's mvn version:set -DnewVersion=... in the root folder:
- root
- sub-1
- sub-2
If the subs are just childs (with <parent>root</parent>), just the root POM's <project>/<version> is updated. The subs <parent>/<version>s are not!
If the subs are childs and modules, the root POM's <project>/<version> and the subs <parent>/<version>s are updated.
If the subs are just modules only the root POM's <project>/<version> is updated as expected.
This is different to your result but it's no less strange because versions:set says:
Sets the current project's version and based on that change propagates that change onto any child modules as necessary.
The sub modules were childs at each of two topmost runs mentioned above.
UPDATE
The reason for the parent POM not updated can be that 9.9.9.9 is not a standard Maven version:
Maven: The Complete Reference, 3.1.1 Project versions:
A project’s version number is used to group and order releases. Maven versions contain the following parts: major version, minor version, incremental version, and qualifier. In a version, these parts correspond to the following format:
<major version>.<minor version>.<incremental version>-<qualifier>
Developing Applications Using Continuous Integration, 7.1 How Version Numbers Work in Maven:
If you use a nonstandard versioning scheme, Maven release and version plug-in goals might not yield the expected results. Because basic string comparison is performed on nonstandard versions, version comparison calculates the order of versions incorrectly in some cases.

What are .pom files in Maven build artifacts?

You can see from browsing any repository that Maven build artifacts contain .pom files. The contents of these files look a whole lot like pom.xml files. Where do these files come from? What are they used for? Additionally, build artifacts have maven-metadata.xml files, at least on search.maven.org, and these files have substantially the same content as the .pom files. What's the deal with that?
The files are the pom files from within the project. Those are deployed to the maven repository during the release build or by other build tools as well (gradle, ivy, etc.).
Those files are needed to describe the dependencies of the appropriate artifact otherwise you have no other opportunity to store such kind of information.
In your particular example (really old 2005) this is a pom file which is created at a time of times where maven was not such distributed. In this case the file does not contain any dependencies.
If you take a look here:
http://search.maven.org/#browse%7C-77609479
you see a number of versions of a single artifact. If you now take a look into the maven-metadata.xml you will see list of available versions.
The best answer I have found so far is in this SO thread. Here is an exact quote, highlighting the crux of the explanation:
Every jar needs to have a pom file describing it, you can just add something simple like this:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>aGroup</groupId>
<artifactId>aArtifactId</artifactId>
<version>aVersion</version>
<packaging>jar</packaging>
<name>a Name</name>
</project>
Another good explanation:
POM that is installed to Nexus will describe the jar. Used to pull the
dependencies that are associated to corresponding jar. When we add the
jar as dependency to our project, all the jars required for the
included jar will be identified through the corresponding pom.

Maven: Selecting Parent Project Based On Profile

I have a maven project - it is a plugin for jenkins. It's parent should be a:
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.414</version>
</parent>
But at the same time this plugin can be also used for hudson, without changing any line of code. But the parent project for it should be:
<parent>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>hudson-plugin-parent</artifactId>
<version>2.0.1</version>
</parent>
Can I specify 2 different profiles for that and use them to build plugin for jenkins or hudson accordingly? So that I call something like that:
mvn package -P jenkins
or
mvn package -P hudson
I have tried to specify properties in profiles, but those are not replaced by their values inside the <parent> tag. So is there any other possibility to build plugin for both, but with as much as possible common code and files?
Added: So, if I cannot do that, what should I do then? How to refactor? What the new structure should be?
As already mentioned, this is not possible.
Also, it is not possible to set a property for the parent's version as the interpolation for that happens a lot earlier than the handling of the profiles.
I would suggest that you create a masterbuild project as follows:
master
|-plugin-jenkins
|-plugin-hudson
|-plugin-assembly
The master should build all three as usual. However, in the assembly, you could add each of the two plugins as dependencies in separate profiles. And... each of these plugins can have the parent you like.
This is obviously somewhat a deviation from the Maven convention, but I believe it is a solution to your problem.
It's not possible because the tag "parent" is not available in the profiles section of the pom.
Currently we decided to stick with 1 repository and 2 separate pom.xml files, giving maven key which pom.xml use to build the project.
mvn package -f pom-jenkins.xml
mvn package -f pom-hudson.xml
No you cannot do that. you will have to refactor somehow to avoid the necessity.
As mentioned already not possible. I would suggest to make separate projects for jenkins plugin and hudson plugin. I assume that in not that far future that will not work anymore cause Hudons and Jenkins will diverge.
In general, you should be able to set the {group,artifact}Id and version of the parent POM via Java System Properties or Environment Variables, but it seems there is a Bug in Maven which will only be fixed in 4.x:
https://issues.apache.org/jira/browse/MNG-624
Another solution is to delegate the inclusion of the parent POM to your own parent POMs which you reference in the relativePath element, and change the content of the target e.g. via a symlink or cp command.
So in the main POM you would write:
<parent>
<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>
<relativePath>./my-parent.pom</relativePath>
</parent>
And in my-parent-jenkins you would just put:
<groupId>org.mycompany.project</groupId>
<artifactId>foo-artifact</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.414</version>
</parent>
The same project information with the block for hudson you put in my-parent-hudson.pom.
No you can either use
ln -s my-parent-jenkins.pom my-parent.pom
or
ln -s my-parent-hudson.pom my-parent.pom
to include the respective parent POM without the need to maintain two different main POM files for your project.
In case POM does not exist at the place referenced in relativePath, Maven will look up the POM in the remote repository[1], which is also an easy way to overwrite a parent POM locally.
[1] http://maven.apache.org/components/ref/3.3.9/maven-model/maven.html#class_parent

Resources