tags:

views:

245

answers:

5

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?

+1  A: 

You can do it with the Maven assembly plugin.

Péter Török
How exactly i can do this?
Volodymyr Frolov
@Volodymyr Please check the user's guide I linked, it contains examples as well. If you have a more concrete question then, and tell us more details about your project, we can give you more concrete answers :-)
Péter Török
As far as I understand distribution assemblies it is something final and not reusable. Isn't it? If so they are not suitable to do what I need. With maven-assembly-plugin I can create distribution assembly for resource library. In my war pom I can even use this artifact as dependency. In such a case I will get target\war-artifact-1.0\WEB-INF\lib\resource-artifact-1.0.jar instead of content of distribution assembly unpacked in target\war-artifact-1.0\ folder. In a provided guide I don't find any way to extract distribution assembly into root of web archive.
Volodymyr Frolov
@Volodymyr You can specify the package format of your assembly - it can be a plain directory structure as well. OTOH, I am fairly sure you can unpack a jar into a specific directory with e.g. an ant task executed via the antrun plugin.
Péter Török
@Péter Package format of assembly doesn't matter. Assembly is final and not reusable anyway. It looks like assembly plugin won't work in my case.Unpacking resource jar into src/main/resources folder works fine. I have found this solution and describe it with pom example in separate answer. But this approach has nothing common with assembly plugin :-)
Volodymyr Frolov
A: 

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.

crowne
Yes, I don't need resources to be jarred. I need them to exists in my web folder and web archive to refer them statically, not via the classloader.
Volodymyr Frolov
A: 

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
matt b
I need to have two projects, not one. First project should contain resources. Second project should include resources from first project into war.
Volodymyr Frolov
Then a WAR overlay is what you want
matt b
@matt b Yes, I can see now that overlays is exactly what I need.
Volodymyr Frolov
A: 

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>
Volodymyr Frolov
+4  A: 

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

Pascal Thivent
@Pascal Thivent you seem to be knowing your way arround maven docs, its well written but badly structured if you ask me.. is there a link that contains all documentation from a to z, maybe I find some things that I can use later, maybe even now but I don't know that it exists.. +1
c0mrade
@Pascal Thivent This is it! Thanks.
Volodymyr Frolov
@c0mrade The documentation is something people often complain about so I guess you're right (information is there but hard to find, which is exactly what you're saying). Luckily, I know where to look or what to search now. But there are some good books that can help you, for example http://www.sonatype.com/book/ (available online) or http://www.packtpub.com/apache-maven-2-effective-implementations/book. Highly recommended.
Pascal Thivent
@Volodymyr You're welcome.
Pascal Thivent
@Pascal Thivent thank you pascal I already read the book maven by example(very good book for maven noobs) and I'm yet to read maven-by-reference , I also got a copy of Apache Maven 2 Effective Implementation and I'll read it this weekend hopefully.
c0mrade