Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share javascript code between maven modules

my maven project has the following structure:

  • "base" module -- contains shared java files -- should contain shared javascript files
  • module 1 -- use shared java files as maven dependency -- should use shared javascript files using ?
  • module 2 -- use shared java files as maven dependency -- should use shared javascript files using ?

Currently, webpack seems to be the new thing in javascript packaging and npm seems to be a proper package manager. So i tried the following: - base modules creates a npm bundle (with npm pack) using webpack - modules 1 and 2 install this bundle manually using a relative path to the base module target folder where the npm package is

Why didn't i use npm publish? - Its not possible to update published npm packages, so every build would need to create a new version number - It would need an internet connection to build

Other options? - I thought about using the maven resources plugin, but this seems to be a lot of manual work included (file names, folders, etc.)

So what I'm asking for is: Do you share javascript code between maven modules within the same project? How do you achieve that? There has to be a better way to do that, or?

If you want to look at my project, take a look here: https://github.com/stefanrinderle/softvis3d

Thanks in advance for your answers an comments!

like image 245
user984200 Avatar asked Jan 09 '16 09:01

user984200


People also ask

How do I share resources between projects in Maven?

There are a few ways to share resources across multiple projects or modules: Cut and paste them. Use Assembly and Dependency plugins. Use the maven-remote-resources-plugin.

How do I merge two Maven projects?

let's assume that you have two maven projects A and B. Now create a new project C in your intllij-idea. In project C, create a new module A1. Copy the dependencies and build, properties (if present) from pom.

How can I add an existing Maven project to another Maven project as a module?

Just do a regular "Import existing maven project into workspace" to get this done. Show activity on this post. If you use M2e with Eclipse you do not need to do that, because Eclipse resolves dependencies across the workspace. You just need to have the two projects open and your dependencies declared correctly.


2 Answers

Basically to redistribute npm modules separately you should use npm publish. During dev time, though, npm has nice feature npm link, that links your local folder as global dependency and makes npm use it instead of downloading from repository. So, I've just added npm link to your project and changed node installation directory so all submodules use the same node instance. This way you can develop your modules easily and publish whenever you're ready.

base/pom.xml:

<plugin>
  <groupId>com.github.eirslett</groupId>
  <artifactId>frontend-maven-plugin</artifactId>
  ...
  <configuration>
    ...
    <installDirectory>../node</installDirectory>
  </configuration>
  <executions>
    <execution>
      <id>npm link</id>
      <goals>
        <goal>npm</goal>
      </goals>
      <configuration>
        <arguments>link</arguments>
      </configuration>
    </execution>
  </executions>
</plugin>

..module/pom.xml

<plugin>
  <groupId>com.github.eirslett</groupId>
  <artifactId>frontend-maven-plugin</artifactId>

  <configuration>
    <installDirectory>../node</installDirectory>
  </configuration>
  <executions>
    <execution>
      <!-- link before install -->
      <id>npm link softvis3d-viewer</id>
      <goals>
        <goal>npm</goal>
      </goals>
      <configuration>
        <arguments>link softvis3d-viewer</arguments>
      </configuration>
    </execution>
    <execution>
      <id>npm install</id>
      <goals>
        <goal>npm</goal>
      </goals>
    </execution>
    ...
  </executions>
</plugin>

..module/package.json

  "devDependencies": {
    ...
    "softvis3d-viewer": "0.0.4-SNAPSHOT"
  },
like image 126
Dzmitry Paulenka Avatar answered Oct 05 '22 23:10

Dzmitry Paulenka


NPM works if the Maven module supplies JavaScript that will undergo transpiling / minification / concatenation. If sharing of pre-compiled modules is required, I successfully use web-fragment.xml (defined in Servlet 3.0 specs) and put my JavaScript etc. under META-INF/resources. Depending on your front end technology, you will need to use some approach relevant to code splitting, or web components.

In essence, client side will need to load and execute your library code before running anything that depends on it.

If you have a small(-ish) library with specific API you want to share between your Maven modules, and it is all part of the same eco-system, npm publish and npm link is a way to go. For bigger chunks of functionality, especially ones that have specific usage profiles (say, user profile editor or configuration page - not used every time), those can be dynamically loaded).

While nothing in it is new, this area (POMs, JARs, NPMs and JS/ES6 modules) seems to be in a search of the best approach and it will take some time to settle on a couple of robust techs. I don't know a clean, absolutely no band-aids, set of rules and settings to deal with it.

like image 25
Alex Pakka Avatar answered Oct 05 '22 22:10

Alex Pakka