2

i have an ant build that just generates a jar full of html pages. i would like to do the same with maven. im currently copying the files over to a new folder but the files are not being added to the final jar. when i run the command
jar tf mavenjar.jar
i dont see the files listed in my terminal. it looks like so: click here
when i run the same command on the ant produced jar, i see the following: click here here's my pom

<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>try1</groupId>
    <artifactId>try1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>mavenjar</finalName>
        <resources>
            <resource>
                <directory>${basedir}/moveTheseFiles</directory>
                <targetPath>${basedir}/newFileLocation</targetPath>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>


any idea on what i may be missing to include the moved files to jar?

2 Answers2

3

There are two ways to achieve this. Following Convention over Configuration - or configuring it.

Convention over Configuration

The easiest way to copy static files into your jar, is using the default folder src/main/resources. All files you add there, will be added into your jar file. Your pom.xml can be reduced to 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>
    <groupId>try1</groupId>
    <artifactId>try1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <finalName>mavenjar</finalName>
    </build>
</project>

Non-default folder

When you really need to have your files in another folder, your configuration is nearly fine. Just the includes part is missing. You have to tell maven to include all files in the specified folder.

<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>try1</groupId>
    <artifactId>try1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <finalName>mavenjar</finalName>
        <resources>
            <resource>
                <directory>moveTheseFiles</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

Note: It seems as if you wanted to create a web application. Maybe you want to create a war file. To achieve this, you can simply set

<packaging>war</packaging>

in your pom.xml. Then all web assets (HTML-pages, CSS, ...) should be located under src/main/webapp and will be copied into the war's root folder. Be aware, that files in src/main/resources will be copied to WEB-INF/classes in the resulting war.

SilverNak
  • 3,283
  • 4
  • 28
  • 44
1

i actually got it work following this solution: Create zip in maven with additional files next to the jar

i created a bin.xml file in this location src/main/assembly. the bin.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>bin</id>
  <baseDirectory>/</baseDirectory>
  <formats>
    <format>jar</format>
  </formats>
   <fileSets>
    <fileSet>
      <directory>${basedir}/dist/newLocation</directory>
      <outputDirectory>/</outputDirectory> 
    </fileSet>
  </fileSets>
</assembly>

and my pom.xml looks like so:

<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>try1</groupId>
    <artifactId>try1</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <finalName>mavenjar</finalName>
        <resources>
            <resource>
                <directory>${basedir}/WWW</directory>
                <targetPath>${basedir}/dist/newLocation</targetPath>
                <filtering>false</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/bin.xml</descriptor>
                    </descriptors>
                    <appendAssemblyId>false</appendAssemblyId>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
                        <phase>package</phase> <!-- append to the packaging phase. -->
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

in the terminal i run

mvn clean package
cd target
jar tf mavenjar.jar

and the output looks like this click here

  • If you solved your problem with this solution you can accept it by clicking the green check mark. That tells other users that this solution worked for you and it's easier to see that you don't need any additional help with this problem. – Secespitus Aug 16 '18 at 09:39