1

I am a newbie on Maven 2 and trying to pack a set of javascript (like Dojo library) into a jar lib using maven(no java class but only files with .js extension). So I guess I should try maven-assembly-plugin, here are a few concerns:

  1. There is no mainClass but how can I still have a MANIEST.MF?
  2. Can I use jar-with-dependencies as <descriptorRefs> if I don't plan to change the file hierarchy in the jar?
  3. May be this is a silly question but where shall I put the pom.xml(within the javascript lib folder or the same level of the javascript lib folder)?

Please enlighten me, thanks in advance.

Dreamer
  • 7,333
  • 24
  • 99
  • 179
  • Do you want to include the JS file to the JAR-artifact of an existing Java Maven project or would you like to produce a jar with nothing but the JS file in it? – BlueDog Jul 31 '12 at 17:34
  • @Tim I want to pack a javascript lib as a jar file, not include the js file in a existing jar file. – Dreamer Jul 31 '12 at 17:38

3 Answers3

6

If you use a standard Maven project layout like so:

project
   |-src
   .   |-main
   .      |-resources
   .         |-<JS files here>
   |-pom.xml

Then a normal POM file that uses the JAR packaging will place your Javascript files into the output JAR.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.foo.bar</groupId>
  <artifactId>foo</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Foo</name>

</project>

This will preserve any folder structures present under src/main/resources.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Thank you for the details. One question to the solution is what about the js files are in a relatively complicate hierarchy(like Dojo or jQuery UI) so shall we need to reassign the path in `assembly.xml` one by one or we assign the class path to parent folder then all the descendant folders are follow the parent class path? Thank you. – Dreamer Jul 31 '12 at 19:16
  • 1
    Yes, this will handle a series of nested folders under the `resources` directory and will preserve the layout in the JAR. – Duncan Jones Jul 31 '12 at 19:40
  • 1
    Is the assembly part really necessary? I think having a simple "packaging=jar" pom, with just the *.js into src/main/resources would get the job done, wouldn't it? – Tony Lâmpada Aug 01 '12 at 06:09
  • @TonyLâmpada You are quite correct! I've simplified my answer, thank you. – Duncan Jones Aug 01 '12 at 06:27
  • What if I want to put a JS file that does not follow this hierarchy? – Hadesara Oct 25 '13 at 23:36
2
  1. mainClass is not a mandatory entry in the manifest.mf, here is a minimal manifest.mf sample from a typical open source library

    Manifest-Version: 1.0
    Archiver-Version: Plexus Archiver 
    Created-By: Apache Maven 
    Built-By: hudsonagent 
    Build-Jdk: 1.5.0_15 
    
  2. I guess this option will probably change the file structure for you, try it out to see whether you can work with the result.

  3. See the standard maven project structure http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
crowne
  • 8,456
  • 3
  • 35
  • 50
1

Maven really isn't the right packaging tool for this. You probably want to use NPM or YARN which are JavaScript specific packaging tools.

https://www.npmjs.com/

https://yarnpkg.com/lang/en/

If you really want to package JavaScript code into a Jar, you can use the src/main/resources directory as indicated in the standard directory layout: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

You can also modify where Maven finds resources: https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html

Maven and Java support another package type called a WAR which is designed for packaging frontend code (HTML, JS, CSS, images, etc...) along with backend Java code: https://maven.apache.org/plugins/maven-war-plugin/usage.html

Scott Boring
  • 2,601
  • 1
  • 25
  • 31