views:

115

answers:

2

What is the best way to put javascript/html/css code in the maven repository, so that is easily usable by java projects.

Is there a way to do it such that the included project can be easily made "web-visible" by the including project?

For example assume I write a very useful tricks.js file an put it in the mvn repository.

Is it possible to create a web project that adds tricks.js as a dependency and then doing

<script src="/some/thing/tricks.js"/>

causes the tricks.js file to be served?

+2  A: 

You can pack it into jar and then unpack it by maven plugins.

merin
Yes, that is the best practice.
SingleShot
I use this practice with GWT.
merin
Although I have never needed to do this myself, this seems like the best approach with Maven's architecture and standards.
aperkins
+6  A: 

External resources should be packaged into an artifact and published to the repository (for simplicity use a jar artifact, but you could specify an assembly to package a zip instead to make it clear what the artifact is for). The maven-dependency-plugin unpacks the jar's contents into a specified location in the war project. That folder is then specified as an external web resources directory for the maven-war-plugin.

The following configuration will unpack the contents of my-resources into the target/external-resources directory, then include the contents of that folder in the war as if they'd been defined in src/main/resources.

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-dependency-plugin</artifactId>
  <executions>
    <execution>
      <id>unpack</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>unpack</goal>
      </goals>
      <configuration>
        <artifactItems>
          <artifactItem>
            <groupId>name.seller.rich</groupId>
            <artifactId>my-resources</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
            <overWrite>false</overWrite>
          </artifactItem>
        </artifactItems>
        <outputDirectory>${project.build.directory}/external-resources</outputDirectory>
        <overWriteReleases>false</overWriteReleases>
      </configuration>
    </execution>
  </executions>
</plugin>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.0</version>
  <configuration>
    <webResources>
      <resource>
        <directory>${project.build.directory}/external-resources</directory>
      </resource>
    </webResources>
  </configuration>
</plugin>
Rich Seller