maven assembly plugin excludes .gitignore - maven

Maven assembly plugin exludes .gitignore files in the final zip package. How do I include .gitignore in the final output zip?
I tried include **/*, but didn't work.
My assembly file:
<id>assembly</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/work</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>How*.html</exclude>
</excludes>
</fileSet>
</fileSets>`
Assembly plugin:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<descriptor>assembly/config.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>build-archive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

After some research, I found that .gitignore and other source repository control files (.svn) are excluded in the final package. Maven does this by default. I had to explicitly set the flag - false to include .gitignore files.
As per maven documentation - useDefaultExcludes -
Whether standard exclusion patterns, such as those matching CVS and
Subversion metadata files, should be used when calculating the files
affected by this set. For backward compatibility, the default value is
true
.

Three suggestions:
1- Try files element beside fileSet in your assembly file. It specifies which single files to include in the assembly. A file is specified by providing one or more of file subelements.
2- Set baseDirectory properly. for example
<baseDirectory>../</baseDirectory>
3- Or, set includeBaseDirectory to true
<includeBaseDirectory>true</includeBaseDirectory>

Think of that, that Linux and MacOS don't show files in the Finder that start with a dot.
Use: Command + Shift + "."
to show hidden files.
Then I would also use :
<files>
<file>
<source> 'absolute or relative path' </source>
</file>
</files>
If you think that still does not work and its still ignoring .gitignore
Try this workaround:
Rename your .gitignore file to _gitignore_
enter image description here
If the gitignore is now in your zip as "my-project.jar" than its working.
If you set destination name to .gitignore and its just not showing up -> its hidden.
Use: Command + Shift + "."

Related

Zip file empty using maven assembly plugin in pom

my pom.xml but i have written something like
.......
..........
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<finalName>${project.artifactId}</finalName>
<descriptor>assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>create-archive</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
..........
.......
My assembly.xml is
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>${project.version}</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.basedir}/${create.stage.directory}</directory>
<includes>
<include>*.*</include>
</includes>
<outputDirectory>${project.basedir}/${create.release.directory}</outputDirectory>
</fileSet>
</fileSets>
</assembly>
This creates an EMPTY foldername.zip in target!!NOT IN THE OUTPUT FOLDER LOCATION THAT I HAVE GIVEN.Is it always target? cant i override??
But in the given directory path I have 3 folders (in which some files) and readme.txt.I just tried giving *.txt inside include tag still I got empty zip folder. I initially guessed that my directory path and output directory path can be wrong.I directly hardcode still there is no luck.
Please help (This question is not duplicate but similar, and I have tried almost all stackoverflow questions related to this.they haven't solved anything)
For details see http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html#class_fileSet
Your descriptor is now configured like this:
create a zip called ${project.artifactId}
from the directory ${project.basedir}/${create.stage.directory} put all files with an extension (not directories, since it says *.*, not something like **/*.* in the ${project.basedir}/${create.release.directory} folder inside the zip.
Especially the last part is weird. Where in the zip should these files end? It should be a relative path to make it predictable.
Don't like the zip file to be created in target? Set the outputDirectory in the plugin configuration, not in the assembly descriptor.

Maven assembly plug-in - how to produce artifact with custom file extension?

I am producing an artifact named foo.bar.zip from the following...
My pom.xml plug-in entry looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptor>src/assembly/bin.xml</descriptor>
<finalName>foo.bar</finalName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
My descriptor file looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<assembly>
<formats>
<format>zip</format>
</formats>
<fileSets>
etc. etc.
My question, is how do I produce a file with a custom extension? E.g. with the name foo.bar instead of foo.bar.zip.
I solved this by combining:
maven-assembly-plugin
copy-rename-maven-plugin
build-helper-maven-plugin
The assembly plugin has a configuration option to not attach the file, so I do that:
<configuration>
...
<attach>false</attach>
</configuration>
Then I use the copy-rename-maven-plugin to rename the file, produced by assembly-plugin, from zip to my custom file type (in my case it is CLI).
After that I use build-helper-maven-plugin to attach the artifact with the custom file type.
Assembly supports just these formats:
"zip" - Creates a ZIP file format
"tar" - Creates a TAR format
"tar.gz" or "tgz" - Creates a gzip'd TAR format
"tar.bz2" or "tbz2" - Creates a bzip'd TAR format
"jar" - Creates a JAR format
"dir" - Creates an exploded directory format
"war" - Creates a WAR format
You should consider to use antrun plugin in a later goal/phase to rename file extension

How to include uncompiled java files in a JAR built by Tycho?

I have a source root with *.java files, but I don't want them to be compiled. Instead, the *.java files should be copied into the jar as they are. The use case for this is that the *.java files are templates and hence should be preserved as they are.
To achieve this, I tried to exclude a source folder from compiling phase of my build and I am quite confused by the official documentation to the Tycho OSGi Compiler Plugin. It says I can use parameter excludeResources but I don't really know how to handle all these parameter types. pom.xml is a structured text file and not a source file, that's why I don't understand how to use java.util.Set for that parameter.
My POM goes like this:
...
<build>
<plugins>
...
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>0.21.0</extensions>
<configuration>
<excludeResources>
<!-- Set of folders consisting of a source folder named "res"
which should be excluded completely from compilation -->
</excludeResources>
</configuration>
</plugin>
</plugins>
</build>
Is it a right approach? If yes, how would I complete the configuration?
Try something like this to remove any occurences of "res" folder and files:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<excludeResources>
<excludeResource>**/res</excludeResource>
</excludeResources>
</configuration>
</plugin>
or this, alternatively, for example, to include the "res" folder and exclude .jar files:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<includes>
<include>res</include>
</includes>
<excludes>
<exclude>**/*.jar</exclude>
</excludes>
</configuration>
</plugin>
Or, to exclude everything that is not the "res" folder:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-compiler-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<excludeResources>
<excludeResource>!**/res</excludeResource>
</excludeResources>
</configuration>
</plugin>
If you want to exclude a whole source root folder from compilation, simply do not add it to any of the src.* entries in build.properties, see [1]
If on the other hand you want to include the *.java files in the source root folder in the resulting jar, add the root folder to the list of bin.includes in build.properties.
excludeResources is unrelated to your problem, as the docs you linked say:
"A list of exclusion filters for non-java resource files which should not be copied to the output directory."
[1] http://eclipse.org/tycho/sitedocs/BuildProperties.html

Maven SNAPSHOT jar file names not consistent using Maven Assembly in MANIFEST file

Here is the scenario:
Two Maven 3 project builds.
Build 1 has snapshot jars that get deployed to Nexus.
Build 2 has dependencies on the snapshots, referenced like 1.0.0-SNAPSHOT, that gets packaged up and zipped up using the mvn clean package assembly:single command.
The issue that we run into:
Occasionally when the assembly is being created, the MANIFEST file for the jar will sometimes say some.jar.1.0.0-SNAPSHOT and sometimes it will say some.jar.1.0.0-datetime stamp, thus causing class not defined errors.
Is there a way to prevent this naming issue in the manifest file?
--edit--
Further research has discovered the following:
"If the snapshot was resolved from a repo then it will be timestamped,
if it came from the reactor or local repo, then it will be -SNAPSHOT.
The plugin calls into the maven resolution logic so this is core maven
behavior. "
This is the exact issue that is being run into. The 2nd build manifest file always has an entry of ./lib/Framework-1.0.0-SNAPSHOT.jar where as the actual jar file name changes between ./lib/Framework-1.0.0-SNAPSHOT.jar and ./lib/Framework-1.0.0-timestamp.jar based on the quote above.
In <dependencySet> you need to set <outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}.${artifact.extension}</outputFileNameMapping>
for example:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
<id>appserverB</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>/lib</outputDirectory>
<outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}.${artifact.extension}</outputFileNameMapping>
<includes>
<include>application:logging</include>
<include>application:core</include>
<include>application:utils</include>
<include>application:appserverB</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
If you are using one of the built-in assembly descriptors you will need to replicate it for your self and add in the outputFileNameMapping entry yourself
For people who use maven-jar-plugin to create the artifact which is then packed by the maven-assembly-plugin and you still see timestamps in the classpath in artifact names, you can disable that by setting useUniqueVersions=false, as follows:
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>${maven-jar-plugin.version}</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>${project.build.finalName}.lib/</classpathPrefix>
<mainClass>com.nate.Application</mainClass>
<!-- To force the use of '-SNAPSHOT' version naming, simply disable the <useUniqueVersions> -->
<useUniqueVersions>false</useUniqueVersions>
</manifest>
<manifestEntries>
<buildTime>${maven.timestamp}</buildTime>
</manifestEntries>
</archive>
</configuration>
use <useBaseVersion>false</useBaseVersion>, when you need copy dependencies.
For example :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
<useBaseVersion>false</useBaseVersion>
</configuration>
</execution>
</executions>
</plugin>
For those who run into this and see the answer from Stephen Connolly but still end up having exceptions, this might be due to the fact that some dependencies have classifiers in their names. Then you'll have to adapt the pattern to use the dashClassifier as an optional value:
<outputFileNameMapping>${artifact.artifactId}-${artifact.baseVersion}${dashClassifier?}.${artifact.extension}</outputFileNameMapping>
This will work even if the dependecy used does not have a classifier.
See the documentation for the assembly plugin for further details.
<useBaseVersion>false</useBaseVersion> Did the trick for me. I just switched to a SNAPSHOT branch and it was including the timestamps.
Major advantage when using snapshots is one can refer to the actual date/time the snapshot was build.

How do I set directory permissions in maven output?

I am using the maven-assembly-plugin to package my build.
I am able to perform some copying of file sets and modify file permissions fine, but I am unable to modify directory permissions. From the documentation, I am trying to use on the directories I care about. However, regardless of what permissions I specify, directories are ALWAYS created based off of the current umask (0022).
Does anyone know of a clean way to modify directory permissions in this way during a Maven build. The only thing that works is umask 0, but I would rather not be forced to do this, since everyone working on this project would have to have this set.
Example maven assembly.xml:
<?xml version="1.0"?>
<assembly>
<id>zip-with-dependencies</id>
<formats>
<format>dir</format>
<format>tar.gz</format>
</formats>
<includeBaseDirectory>true</includeBaseDirectory>
<dependencySets>
<dependencySet>
<includes>
<include>foo:bar</include>
</includes>
<outputDirectory>/resources/blah</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${basedir}/src/main/web</directory>
<includes>
<include>some_dir</include>
</includes>
<outputDirectory>web</outputDirectory>
<fileMode>0777</fileMode>
<directoryMode>0777</directoryMode>
</fileSet>
</fileSets>
</assembly>
I had the same problem. I tested all the above solutions and none of them worked for me.
The best solution I had in mind and that worked for me was to pre create these parent folders as empty folders, before actually writing to them.
So, to relate to the original problem, you should use:
<fileSet>
<directory>./</directory>
<outputDirectory>/resources</outputDirectory>
<excludes>
<exclude>*/**</exclude>
</excludes>
<directoryMode>0700</directoryMode>
</fileSet>
This should be put before the actual copy to the subfolder of resources in your example.
./ - is simply some existing folder. It can be any other folder, as long as it exists. Note that we exclude any file from the fileSet.
So the result would be an empty folder with the appropriate set of permissions.
On a side note, whoever uses tar to pack the files, without this set, the tar file won't have the permissions set for this parent folder. So extraction will result with a new folder, but with permissions of the extracting user + his umask.
0700 was used only for the sake of the example, of course.
I've solved this problem with a combination of settings in the pom.xml and the assembly descriptor.
In pom specify the defaults for the entire zip file.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2.2</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptors>
<descriptor>src/main/assembly/descriptor.xml</descriptor>
</descriptors>
<archiverConfig>
<directoryMode>0755</directoryMode>
<defaultDirectoryMode>0755</defaultDirectoryMode>
<fileMode>0644</fileMode>
</archiverConfig>
</configuration>
</plugin>
Then in the assembly descriptor I provide the overrides for individual folders that shouldn't have the default permissions.
<fileSets>
<fileSet>
<directory>src/conf</directory>
<outputDirectory>conf</outputDirectory>
<fileMode>0600</fileMode>
<directoryMode>0700</directoryMode>
</fileSet>
<fileSet>
<directory>src/db</directory>
<outputDirectory>db</outputDirectory>
</fileSet>
<fileSet>
<directory>src/bin</directory>
<outputDirectory>bin</outputDirectory>
<fileMode>0755</fileMode>
</fileSet>
Here the files in the bin directory will be given executable state for all users. The conf directory and files in it are accessible only by the owner, and the db directory inherits the permissions from the settings in the pom.
The assembly file settings are described at http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html
I couldn't find any official documentation on the configuration section other than the JIRA issue that amra identified.
I found a JIRA issue describing this behavior. A workaround should be
<configuration>
<archiverConfig>
<fileMode>420</fileMode> <!-- 420(dec) = 644(oct) -->
<directoryMode>493</directoryMode> <!-- 493(dec) = 755(oct) -->
<defaultDirectoryMode>493</defaultDirectoryMode>
</archiverConfig>
</configuration>

Resources