Exclude hidden folders to generate archetype - maven

I'm using the command:
mvn archetype:create-from-project -Darchetype.properties=./archetype.properties
I want exclude hidden dirs, for example .sonar, I try the follow options:
excludePatterns=**/*sonar/*
and
excludePatterns=**/.sonar/*
but not working form me, with non hidden dirs work well.
How can I exclude hidden directories?

There is a bug in version 2.4 of the Archetype Plugin in which certain files that match the exclude patterns are still included. I created ARCHETYPE-513 to track this and this bug has been fixed for version 3.0.0 of the plugin. Thus, make sure you use this version by specifying in your POM:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-archetype-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
To recap, the excludePatterns property should be present in the property file, configured with propertyFile or the -Darchetype.properties user property, that is used by the plugin. So the following archetype.properties property file:
excludePatterns=**/.sonar/**
used with mvn clean archetype:create-from-project -Darchetype.properties=archetype.properties will correctly exclude all folders named .sonar that are present in your project when the archetype is created.

Related

maven-jar-plugin does not include .gitignore file

I try to package an application into a jar file with maven. Somehow all files except .gitignore files are added to the jar.
Why is this file skipped and how can I disable this?
Even if I try to include it like below the include is ignored and the jar file remains empty.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<includes>
<include>**/.gitignore</include>
</includes>
</configuration>
</plugin>
maven-jar-plugin version: 3.1.0
maven version: 3.5.2
I tried this with a src/main/resources/.gitignore and it worked with the default maven-jar-plugin:2.4, i.e. .gitignore was packaged into the JAR.
Then I used the maven-jar-plugin:3.1.0 you mention and it did not work, as you describe.
It turned out that it doesn't work from v2.5 onwards.
I have the same issue with a .metadata folder in the target/classes folder. The .metadata folder is not included in the jar archive.
For me, it is not working with maven-jar-plugin:2.4 and upper. With version 2.3 it is working.
I submitted this issue : https://issues.apache.org/jira/browse/MJAR-265
The first thing is using a jar file example projects is astonishing. I would never expect to have example projects within a .jar file. The intention of a jar files is something different. I would suggest to use something more appropriate like .zip or .tar.gz etc. (This can be achieved with the maven-assembly-plugin) This will prevent accidental not intended use.
Apart from the whole problem is based on the definition of resources which are usually copied from src/main/resources to the target/classes directory. This is done by the maven-resources-plugin.
The maven-resources-plugin plugin has some kind of configuration which excludes some files which are usually not copied which contains .gitignore. So this means just putting a .gitignore file into src/main/resources will not produce the expected result nor using <includes>..</includes> configuration will not help here as well.
This means you need to change the default configuration of maven-resources-plugin via pluginManagement section like the following:
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<addDefaultExcludes>false</addDefaultExcludes>
</configuration>
</plugin>
Than the .gitignore file will be copied and should be packaged into the resulting jar file (Which I would not recommend to do.)

Checkout file from another repo using maven-scm plugin

I have a java application that is in git repo RepoA and has a scm configuration set up for this repo for maven-release plugin etc.
I want to fetch one file from another RepoB (it is fine to checkout the whole repo also because there is only 1 file there) and use it as a part of build step.
How to do it with maven-scm plugin if scm section is already set up for RepoA?
Thanks.
You can use a separate maven profile for this task.
Here's profile part from pom.xml, assuming that you want to fetch file foo/bar.txt from github repo github-user/some-repo:
<profile>
<id>checkout-foo-bar</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-plugin</artifactId>
<version>1.11.2</version>
<configuration>
<connectionUrl>scm:git:git#github.com:github-user/some-repo</connectionUrl>
<includes>foo/bar.txt</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Then run mvn scm:checkout -P checkout-foo-bar
Plugin first fetches all the files from repo and then removes ones that you don't need. This takes extra time, especially if the repo is huge.
I didn't find a way to setup output directory other than default target/checkout. But hopefully this working example can be a good starting point to solve a problem.

changing maven site plugin source folder

I need to change the maven site documentation source folder. I've check the plugin documentation and couldn't find this configuration.
Problem scenario:
I have a maven project with several child project, and one of this child is only a angular2 project.
On the angular project, the source folder has the pom.xml and several angular files, and one of the folder used by angular is 'src' (which is there the components, services, etc are stored).
I need to add maven site to this project, and it look the source files into 'src/site/*'. This works, but this 'site' folder is mixed with several ts files, and this looks messy, so I need to change the default folder for maven site plugin.
Is there any way to change it?
If you wish to change the source directory in which Maven looks for, although I would rather recommend to clean your directory, I think what's below should work:
<build>
....
<plugins>
....
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.4</version>
<configuration>
<siteDirectory>my/site/dir</siteDirectory>
</configuration>
</plugin>
....
</plugins>
....
</build>

Wildfly Deploy Maven - Remove Version

I want to deploy a war that I have created using maven to wildfly using the wildfly-maven-plugin.
The final name of the war is something like: my-war-1.0.war
The war also contains a jboss-web.xml specifying the context root (e.g. /my-war)
Problem Description
If I now deploy the war to wildfly I will get a "my-war-1.0.war" deployment.
If I later want to deploy a new version (e.g. the war is now named my-war-1.1.war) I get a conflict as the context root is already known but the deployment has a new name.
Is there a way using the wildfly-maven-plugin to deploy a "my-war.war" instead?
I need to keep the original final build name inside the maven build for versioning and deploying to our nexus.
The simplest solution is to use the <finalName/> element on the <build/> configuration.
<build>
<finalName>${project.artifactId}</finalName>
</build>
You can use the maven war plugin to rename the final war. For Eg:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>my-war</warName>
</configuration>
</plugin>
</plugins>
</build>
This will always generate the war with the name my-war.war in your "target" directory.
I found out that I can use the parameters <name/> and <runtimeName/> inside the <configuration/> of the maven-wildfly-plugin.
That way I can specify what the deployment should be called on the server and each time just replace it. It is important to have the two parameters end in ".war", otherwise you will get a 404 error.
Using this method I can keep the original name of the final build result containing the version (my-app-1.0.war) and archive it inside our internal nexus repository.

How to force Maven to always create a new jar file?

If all classes are up-to-date "Nothing to compile - all classes are up to date"
so will maven create jar again?
As I am seeing in my log that jar is not creating again. so maven come to know that all classes are up-to-date.
Question: is there any process or another thing which work on this?
The Maven Jar Plugin will create a jar via its jar goal if none exists or skip its creation if existing but nothing changed.
You can force the creation of the jar via its forceCreation option (since version 2.2). From official documentation:
Require the jar plugin to build a new JAR even if none of the contents appear to have changed. By default, this plugin looks to see if the output jar exists and inputs have not changed. If these conditions are true, the plugin skips creation of the jar. This does not work when other plugins, like the maven-shade-plugin, are configured to post-process the jar. This plugin can not detect the post-processing, and so leaves the post-processed jar in place. This can lead to failures when those plugins do not expect to find their own output as an input. Set this parameter to true to avoid these problems by forcing this plugin to recreate the jar every time.
Its default value is at false, which explains the behavior you are having.
If you want to force it always, you can add to your pom file:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<forceCreation>true</forceCreation>
</configuration>
...
</plugin>
</plugins>
</build>
...
</project>
Or just on a single build, invoke it as following:
mvn package -Djar.forceCreation=true
So, going back to your question:
is there any process or another thing which work on this?
The answer is: Yes, the Maven Jar Plugin works on this and the option above will change its behavior.

Resources