How to get the final result of a multi module project in the root project directory folder? - maven

I have a a multi modules project such as:
project:
- module1
- module2
- packaging
The module 1 and 2 respectively generate a jar and the packaging use the maven assembly to put everything into a zip file.
The result looks like:
final-archive.zip:
- module1.jar
- module2.jar
The issue I'm facing is that the resulting zip is in the packaging folder of my project and not in the root folder of the project directory.
How can I have the final result of my build at the place I wish to ?
The main pom.xml
<?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>
<packaging>pom</packaging>
<name>groupId :: Name :: Project</name>
<groupId>groupId</groupId>
<artifactId>project</artifactId>
<version>1.0.0</version>
<modules>
<module>module1</module>
<module>module2</module>
<module>packaging</module>
</modules>
</project>
The packaging pom.xml
<?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>
<packaging>pom</packaging>
<name>groupId :: Project :: Packaging</name>
<artifactId>packaging</artifactId>
<properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>${maven-assembly-plugin.version}</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>install</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The assembly
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>packaging-assembly</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<files>
<file>
<source>../module1/target/jar-module1-${version}.jar</source>
</file>
<file>
<source>../module2/target/jar-module2-${version}.jar</source>
</file>
</files>
</assembly>

Related

Maven assembly plugin - ignore 'archive cannot be empty'

I have problem with assembly plugin. I have to use one assembly definition which create JAR file for more modules. So project structure looks like this
module/
----module1/
-------- pom.xml
----module2/
-------- pom.xml
----moduleX/
-------- pom.xml
assembly_jar.xml
pom.xml (this is parent)
Parent pom file looks like following:
<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.my.project</groupId>
<artifactId>parent-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<attach>false</attach>
<descriptors>
<descriptor>${source-systems.basedir}/assembly_jar.xml</descriptor>
</descriptors>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
</execution>
</plugin>
</plugins>
</build>
</project>
assembly_jar.xml looks like following:
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
<id>jar-with-some-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<scope>runtime</scope>
<unpack>true</unpack>
</dependencySet>
</dependencySets>
</assembly>
pom file of module 1 looks like this:
<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>
<parent>
<groupId>com.my.project</groupId>
<artifactId>parent-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
<groupId>com.my.project</groupId>
<artifactId>module1</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>oracle_rds</name>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.4</version>
</dependency>
</dependencies>
</project>
When I run mvn clean install for module1 everything works fine and jar file is created. But then I have module2 which does not need any particular dependencies. So pom file looks like this:
<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>
<parent>
<groupId>com.my.project</groupId>
<artifactId>parent-module</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<packaging>pom</packaging>
<groupId>com.my.project</groupId>
<artifactId>module2</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>oracle_rds</name>
</project>
When I run mvn clean install I have error Error creating assembly archive jar-with-some-dependencies: archive cannot be empty It is because there is no dependency to pack into jar file.
Is there any way how to ignore this error and do not create jar file if there are not dependencies in module?
I do not want to move assembly definition into module because of big duplicity (I will have more then 100 modules and mostly of them will have some special dependencies)
Thanks for help.

WEB-INF/classes empty upon MavenBuild

I am building a multi modules project the hierarchy for it as follow :
ParentProject
|_ WebModule
|_src
|_main
|_ java
| |_Some packages here
|_webapp
|_EARModule
|_ EJBModule
|_ JARModule
Mvn clean install command is working perfectly fine and i can find the generated EAR file. Inside the EAR i can see the EJB module and the JAR module are created and compiled as well, but for the web module the WAR is created with correct structure but the class directory is empty !
I am using Maven2 and JAVA7
the parent POM is :
<?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>XXXX</groupId>
<artifactId>parentModule</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>EJBModule</module>
<module>EARModule</module>
<module>JARModule</module>
<module>WEBModule</module>
</modules>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<!--Project dependencies-->
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.9.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
EAR POM :
<?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">
<parent>
<artifactId>ParentModule</artifactId>
<groupId>XXXX</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>ear</packaging>
<artifactId>EARModule</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ear-plugin</artifactId>
<version>3.0.1</version>
<configuration>
<modules>
<webModule>
<groupId>XXXX</groupId>
<artifactId>WebModule</artifactId>
<bundleFileName>abc.war</bundleFileName>
<contextRoot>/abc</contextRoot>
</webModule>
<ejbModule>
<groupId>XXXX</groupId>
<artifactId>EJBModule</artifactId>
<bundleFileName>ejbModule.jar</bundleFileName>
</ejbModule>
</modules>
<displayName>theEar</displayName>
<generateApplicationXml>true</generateApplicationXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!--some dependencies-->
</dependencies>
</project>
WEB app POM :
<?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">
<parent>
<artifactId>parentModule</artifactId>
<groupId>XXXX</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>WebModule</artifactId>
<packaging>war</packaging>
<name>abc</name>
<dependencies>
<!-- some dependencies -->
</dependencies>
</project>
Note : These pom files are samples to demonstrate my case.
The problem was not in the POM files. the actual issue is that i copied source code under src/main/java directory but it was having some compilation issues.
I am using IntelliJ IDE, unfortunately, IntelliJ is not notifying me after copying the source code (even after synchronize the project). So what i did is just right click on the web module then clicked on Build module <'module name'> which shows the compilation error. After rectifying the issues then run
mvn clean install
I could find the classes get generated in the war.
I hope this help somebody

How to create a zip archive with some readonly files with maven-assembly-plugin

I have a project with 2 files (src/foo/a.txt and src/foo/b.txt) and I want to create a zip archive where b.txt is in readonly.
Here's my environment:
Windows 7
JRE 1.8.0_112
Maven 3.3.3
maven-assembly-plugin 3.0.0
7-Zip 9.20
Here's my pom.xml:
<?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>org.foo</groupId>
<artifactId>foo-zip</artifactId>
<version>0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals><goal>single</goal></goals>
</execution>
</executions>
<configuration>
<encoding>UTF-8</encoding>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/foo-assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>
and here's my assembly descriptor:
<?xml version="1.0" encoding="UTF-8"?>
<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>foo-zip</id>
<includeBaseDirectory>false</includeBaseDirectory>
<formats><format>zip</format></formats>
<fileSets>
<fileSet>
<directory>src/foo</directory>
<excludes><exclude>b.txt</exclude></excludes>
<outputDirectory />
</fileSet>
<fileSet>
<directory>src/foo</directory>
<includes><include>b.txt</include></includes>
<fileMode>0444</fileMode>
<outputDirectory />
</fileSet>
</fileSets>
</assembly>
When I unzip the generated archive, and open the properties of b.txt, the "readonly" checkbox is not checked.
I'm aware of:
Maven assembly plugin not applying fileMode on unpacked dependencySet and maven assembly plugin do not set file attributes but they haven't helped me much.
I tried various fileMode values (0444 or 0544) but I just can't get my file to be uncompressed in readonly mode.
Any ideas?

use maven-assembly-plugin , but jar only contain dependency project's classes, no app project classes

I have a library project which I declared as a <dependency> of my App project.
The library project's pom.xml:
<?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>com.my.library</groupId>
<artifactId>MyLib</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>MyLib</name>
...
My App project's pom.xml:
<?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>com.my.app</groupId>
<artifactId>MyApp</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>MyApp</name>
<dependencies>
<!--include library project as a dependency-->
<dependency>
<groupId>com.my.library</groupId>
<artifactId>MyLib</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
Since I need an jar which includes classes of my library project, so I declared also maven-assembly-plugin in my App project's pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptor>assembly.xml</descriptor>
</configuration>
<executions>
<execution>
<id>make-jar</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
The assembly.xml looks like this:
<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>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<!--inclucde classes from my library project-->
<include>com.my.library:MyLib</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
Ok, at this point, I run mvn clean install under my App project, under target/ directory, I got MyApp-jar-with-dependencies.jar.
But when I check the content of above jar, it only contains classes of my library project but doesn't contain my App project's classes? WHY?

when installing artifact *zip file doesn't appear in local repo

I have project with some configuration files and after run maven install I got *.zip archive in target directory. However in my local repo I have only pom file. I auusme it's because of <packaging>pom</packaging> drfined in pom.xml. How can I get *.zip file in local repo anyway?
Here is the pom
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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>
<parent>
<groupId>com.mycomp.name</groupId>
<artifactId>saas-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../saas-parent/pom.xml</relativePath>
</parent>
<!-- TEST -->
<artifactId>htdocs</artifactId>
<name>htdocs</name>
<packaging>pom</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>assembly</id>
<goals><goal>single</goal></goals>
<phase>package</phase>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>${project.basedir}/src/main/assembly/assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
</project>

Resources