开发者

Does maven reusable resource project possible?

开发者 https://www.devze.com 2022-12-21 12:09 出处:网络
Is there any ability to build maven artifact which will contain only resources but no sources and which can be reused by other projects?

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 follow开发者_JS百科ing. 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?


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


You can do it with the Maven assembly plugin.


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.


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


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消