Zip file empty using maven assembly plugin in pom - maven

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.

Related

want different maven project structure

Trying to understand maven flow.Need to create a folder called 'my-source' and copy multiple files in the same folder.
And i want a maven build zip file which should contain 'my-source' folder and its contents.How do i do that?Where ever articles i refer am seeing only java project/folder structure examples.
Also i have seen sourcedirectory/outputdirectory. Not sure where i can modify these values as i couldnot find location of effectivepom
So please give me the process to fulfil my requirement
In order to create a zip file with all your contents you will have to use the maven assembly plugin in your pom.xml. Apart from this, you will need one more file (say bin.xml) that will tell which all files you want to be packaged in the zip. The assembly plugin in the pom.xml will simply point to the bin.xml file.
See a sample code for pom.xml below.
<?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.abc</groupId>
<artifactId>xyz</artifactId>
<version>3.1.3</version>
<build>
<plugins>
<plugin> <!-- Create a zip file with the contents you want deployed -->
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<descriptor>src/assembly/bin.xml</descriptor> // very important. This is where we will tell where our bin.xml is located
</descriptors>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Now create a folder structure like src/assembly and add a file with the name bin.xml. See below for a sample code that should go inside bin.xml.
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>distrib</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<fileSets>
<fileSet>
<directory>${project.basedir}/my-source</directory> // the folder that you want to the packaged in the zip file
<outputDirectory>/</outputDirectory> // the location within the zip file where your folder should be copied. In this case it will place the my-source directory at the root level of the zip file.
<useDefaultExcludes>true</useDefaultExcludes>
</fileSet>
</fileSets>
</assembly>

maven assembly plugin excludes .gitignore

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 + "."

After bumping up version of maven-assembly-plugin from 2.4 to 2.5, files within the assembly are now read-only

I am hoping that someone may be able to explain this change in behaviour of the maven-assembly-plugin w.r.t. file permissions.
This was causing our Eclipse-based application to fail to launch as it was unable to obtain locks on files because of Locking is not possible in the directory C:\eclipse\configuration\org.eclipse.osgi
The original files I am bundling into the built assembly are not read only so I would not expect the files to suddenly become read-only in the zip file. I cannot find any information on changes that might have altered this behaviour; it seems like a regression to me but if it is intentional for whatever reason then users should have been informed about the change in behaviour in the announcement of the release of the 2.5 version of the plugin of in the changelog.
This might be related to the upgrade of the plexus-archiver transitive dependency rather than the upgrade of maven assembly itself.
BTW this is on Windows 7.
Edit: Simple example as requested:
POM configured with a maven-assembly-plugin configured thus,
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5</version>
<executions>
<execution>
<configuration>
<descriptors>
<descriptor>binary.xml</descriptor>
</descriptors>
</configuration>
<id>make-binary</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
referencing an assembly descriptor binary.xml that brings in some arbitrary files, in this case the pom.xml and the binary.xml of the build,
<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>binary</id>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory></directory>
<outputDirectory></outputDirectory>
<includes><include>**</include></includes>
</fileSet>
</fileSets>
</assembly>

Maven overwrite resource file in dependency

I have two Maven modules, A and B. A is a dependency of B. Both modules have a resource file named default.properties located in src/main/resources. I need to keep the filenames the same and the location of the file the same in both projects because both A and B are using code which expects the file to be named and located where it is. When building B, A's default properties is in the final jar. I wish to have B's properties when I build B. How can I do this?
I know this is 3 years old but I had the same problem and this is the closest question I found, but still without correct answer so maybe someone will find it useful.
Example maven-assembly descriptor based on jar-with-dependencies (fixes overriding of log4j.properties by dependencies):
<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>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>false</useProjectArtifact>
<unpack>true</unpack>
<unpackOptions>
<excludes>
<exclude>log4j.properties</exclude>
</excludes>
</unpackOptions>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
<fileSets>
<fileSet>
<directory>${project.build.outputDirectory}</directory>
<outputDirectory>/</outputDirectory>
</fileSet>
</fileSets>
</assembly>
The key is to provide different rules for dependencies and the actual project (top of hierarchy). Those can be split by using <useProjectArtifact>false</useProjectArtifact> and providing separate rules in fileSets for the project. Otherwise none of log4j.properties would be packed, including the top one.
Ok, Maven Resources Plugin and Assembly plugin did not cut it, so I dug some more.
It seems this is doable with Maven Shade plugin.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!-- Main class -->
<mainClass> <!-- fully qualified package and class name --> </mainClass>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</transformer>
</transformers>
<filters>
<filter>
<artifact>org.something:SomeDependency</artifact>
<excludes>
<exclude>*.properties</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
So, inside the <configuration> ... </configuration> -tags I've defined two things: a transformer-implementation that takes care of modifying the jar-manifest to be runnable and use the current directory as classpath root, and excluding all the files ending with .properties from inside of dependency org.something:SomeDependency.
The actual filtering part is where you can exclude the files you don't want to end up in the final jar built by shade. You can exclude the files from all the dependencies and the current project using <artifact>*:*</artifact> inside the defined <filter>, or you can select only certain dependency using <artifact>dependcyGroupId:dependencyArtifact</artifact>, for example <artifact>junit:junit</artifact>, or even using wildcards for one or the other (<artifact>*:junit</artifact>). The excluded files are then defined inside the <excludes>...</excludes> -tags. Again, you can use exact filenames or wildcards. This should get you going with your current problem, although I'd suggest reading the documentation from the plugin-site, because shade can do a lot more than this.

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