Volodymyr Frolov
Volodymyr Frolov

Reputation: 1306

Does maven reusable resource project possible?

Is there any ability to build maven artifact which will contain only resources but no sources and which can be reused by other projects?

Motivation is the following. I have a library which contains only html/css/javascript code. This library must be packed as resources into war project. As for now I build web archive with resources by single pom. But am I able to separate html/css/javascript code into new artifact and reuse it in several war projects?

Upvotes: 5

Views: 2908

Answers (5)

Pascal Thivent
Pascal Thivent

Reputation: 570295

Use Maven Overlays. See Manipulating WAR Overlays for more examples.

Upvotes: 9

Volodymyr Frolov
Volodymyr Frolov

Reputation: 1306

This can be done by jarring resource artifact and unpacking it into src/main/resources in war project during validate phase for example. Resource pom is trivial but war pom will contain the following:

 <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
       <execution>
          <id>unpack</id>
          <phase>validate</phase>
          <goals>
             <goal>unpack</goal>
          </goals>
          <configuration>
             <artifactItems>
                <artifactItem>
                   <groupId>my.company</groupId>
                   <artifactId>resource-artifact</artifactId>
                   <version>1.0</version>
                   <overWrite>true</overWrite>
                   <outputDirectory>src/main/resources</outputDirectory>
                </artifactItem>
             </artifactItems>
          </configuration>
       </execution>
    </executions>
 </plugin>

Upvotes: 0

matt b
matt b

Reputation: 139921

This is a pretty simple thing to test:

$ ls -R
.:
pom.xml  src

./src:
main

./src/main:
resources

./src/main/resources:
README.txt  content-is-here.txt

$ mvn package
... Maven doing it's thing...

$ unzip -l target/test-1.0-SNAPSHOT.jar
Archive:  target/test-1.0-SNAPSHOT.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  02-25-2010 16:18   META-INF/
      123  02-25-2010 16:18   META-INF/MANIFEST.MF
       10  02-25-2010 16:18   content-is-here.txt
        0  02-25-2010 16:18   README.txt
        0  02-25-2010 16:18   META-INF/maven/
        0  02-25-2010 16:18   META-INF/maven/group/
        0  02-25-2010 16:18   META-INF/maven/group/test/
      626  02-25-2010 16:15   META-INF/maven/group/test/pom.xml
      106  02-25-2010 16:18   META-INF/maven/group/test/pom.properties
---------                     -------
      865                     9 files

Upvotes: 0

crowne
crowne

Reputation: 8534

I don't imagine that maven would prevent you from jarring a few resources together, and adding that as a dependency in your web project.

However, the way that you need to reference the resources would be a bit strange. I'm not used to loading css stylesheets as java resources within a jar file within WEB-INF/lib.

I would want to refer to them as normal web resources, relative to the root of the WAR file, not via the classloader.

Upvotes: 0

P&#233;ter T&#246;r&#246;k
P&#233;ter T&#246;r&#246;k

Reputation: 116256

You can do it with the Maven assembly plugin.

Upvotes: 1

Related Questions