Sharing JavaScript code between Maven modules can be achieved by following these steps

My Maven project has a unique structure that includes:

  • "Base" module -- containing shared Java files -- should also include shared JavaScript files
  • Module 1 -- using shared Java files as a Maven dependency -- should utilize shared JavaScript files through ?
  • Module 2 -- utilizing shared Java files as a Maven dependency -- should incorporate shared JavaScript files using ?

Recently, I have noticed that webpack is gaining popularity in packaging JavaScript and npm serves as a reliable package manager. So, here's what I attempted: - The base module creates an npm bundle (using npm pack) with webpack - Modules 1 and 2 manually install this bundle by referencing the relative path to the target folder of the base module where the npm package resides

Why did I avoid using npm publish? - Updating published npm packages isn't feasible because it requires incrementing version numbers for each build - It necessitates an internet connection during the building process

Are there other options available? - One alternative I considered was employing the Maven resources plugin, but it seemed labor-intensive due to the manual aspects involved (such as file names, folder structures, etc.)

So my question is: How do you share JavaScript code among Maven modules within the same project? Is there a more efficient way to accomplish this task?

If you're interested in exploring my project further, feel free to visit: https://github.com/stefanrinderle/softvis3d

I appreciate any insights or feedback you may provide!

Answer №1

When it comes to redistributing npm modules individually, the recommended approach is to utilize the npm publish command. However, during the development phase, you can take advantage of the convenient feature provided by npm called npm link. This allows you to link your local folder as a global dependency, directing npm to use it instead of fetching from the repository. By incorporating the npm link command into your project and adjusting the node installation directory, all submodules will share the same node instance. This streamlined setup enables you to develop your modules seamlessly and proceed with publishing when you are prepared.

In the base/pom.xml file:

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

Within the module/pom.xml file:

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

  <configuration>
    <installDirectory>../node</installDirectory>
  </configuration>
  <executions>
    <execution>
      <!-- linking prior to installation -->
      <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>

And in the module/package.json file:

  "devDependencies": {
    ...
    "softvis3d-viewer": "0.0.4-SNAPSHOT"
  },

Answer №2

If you're looking to make use of NPM within a Maven module for JavaScript transpiling, minification, and concatenation, there are some key considerations to keep in mind. Utilizing web-fragment.xml as specified in the Servlet 3.0 specs can help in sharing pre-compiled modules by placing JavaScript files under META-INF/resources. Depending on your front end tech stack, strategies like code splitting or leveraging web components may be necessary.

It's important for the client-side to load and execute library code before any dependent functionalities are accessed.

For sharing specific APIs across Maven modules within the same eco-system with npm, options like 'npm publish' and 'npm link' can prove useful. However, for larger pieces of functionality that aren't always used, dynamic loading may be more appropriate.

While the concepts of POMs, JARs, NPMs, and JS/ES6 modules are not new, the community is still exploring the most effective approaches. Finding a clean, comprehensive set of rules and configurations to handle these technologies seamlessly is an ongoing process.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing UTC Time with AngularUI's UI-Date Datepicker

My issue lies with the datepicker using localized time instead of UTC when making calls to the backend. To handle this, I've created a function that adjusts the value before posting it to the server: function adjustDateForTimeOffset(dateToAdjust) ...

Ways to invoke a class method by clicking on it

My initialization function is defined as follows: init: function() { $("#editRow").click(function() { <code> } $(".removeRow").click(function() { <code> } } I am trying to find a way to call the class method removeRow directly in the onc ...

Real-time Dropdown Updating in PHP

I apologize if this question has been addressed previously, but I have attempted a search and have not found answers in other posts. The responses I did come across did not pertain to my specific question (which mostly revolved around dropdowns fetching re ...

My component reference seems to have gone missing in angular2

Trying to load my Angular 2 app, I encountered this error: https://i.stack.imgur.com/FmgZE.png Despite having all the necessary files in place. https://i.stack.imgur.com/kj9cP.png Any suggestions on how to resolve this issue? Here is a snippet from ap ...

Modify meta titles according to specific url #id

My website is built in static HTML and our server does not support PHP or C#. Can JavaScript, jQuery, Ajax, or other technologies achieve the following: If the URL is: Https://example.com/page, the meta title will display as "home page". Https://example ...

Arrangement featuring two distinct types of tiles

Looking for a way to achieve this layout using just CSS or a combination of CSS and a little JS. Click on my avatar to see the reference image enlarged =) I've tried using floats and display options but haven't been successful. Take a look at htt ...

Unable to submit form in Nextjs using react-bootstrap

Instructions To create a registration form, follow these steps: Fill out the form on the register page and then click submit. The react-bootstrap button will trigger the handleSubmit() function using onSubmit={}. Expected vs Actual Outcome I attempted va ...

Webpack 2.7.0 throws an error: "Unexpected parameter: theme"

At the moment, I am on webpack 1.16.0 and using --theme as an argument to set the output path and plugin paths. The command appears as: rimraf dist && webpack --bail --progress --profile --theme=<name of theme> However, as I try to upgrade ...

What causes variations in running identical code between the Node environment and the Chrome console?

let myName = "World"; function functionA() { let myName = "FunctionA"; return function() { console.log(this.myName); } } functionA()(); Executing the code above in my terminal with node results in undefined, while running it in Chrom ...

Customizing the styling of a TextField component in ReactJS using material-ui

I am currently working with Reactjs and material-ui. I am looking to apply some custom styles to a TextField using css. Specifically, I would like to change the color of the TextField underline and label when the input is clicked. Although I know it can b ...

Attempting to have this .js lightbox appear as soon as the page loads

This Lightbox is absolutely stunning! However, I am looking for a way to automatically trigger the lightbox when the page loads. ...

Error message: Unable to access properties of null when calling 'registerPlugin' in @devexpress/dx-react-grid-material-ui

I have developed a CustomGrid component that is based on the DevExpress Grid component. So far, it has been working smoothly. Here's how the implementation looks like in App.tsx: import Paper from "@mui/material/Paper"; import { Table, TableHeaderRow ...

Tips for extracting HTML content from JSON data as valid HTML code

I am attempting to extract HTML content from a JSON object response.indexText, which has been validated with JSONLint. { "indexText": "<div id=\"content-home\"><h1> Hello World! <\/h1> <p>Some text.<\/p> ...

Scroll bar malfunction in Highcharts

I am struggling to get the scroll bar working so that all categories can be displayed. I have tried different approaches but haven't been able to figure out where I'm going wrong. See the code in action here: http://jsfiddle.net/manraj/7racxxu0/ ...

Sending data via an AJAX POST request in the request parameter

I have a question regarding the method of making a POST request in my code: $.ajax({ url :"/clientCredentials.json", type: "POST", data: { "clientEmail": email, "clientName":clientName, "orgName":orgName, "l ...

How come my variable doesn't show up in the view even though I filled it in the controller?

I'm having trouble with my AngularJS setup. The angular.min.js file is located in the js folder, following a code example from O'Reilly's book. However, for some reason it is not working on my computer. //controller.js function HelloCont ...

Obtain the specific key's value from a new Map state

In my code, I've defined a variable called checkedItems as a new Map(); When I try to console.log(checkedItem), the output is as follows: Map(3) {"1" => true, "1.5" => true, "2" => false} Is there a way for me to ...

Encountering a 404 error when trying to retrieve webpack.hot-update.json in Next.js version 13.4

I keep encountering a 404 error for http://localhost:3002/_next/static/webpack/cbfe56cfa390e6ae.webpack.hot-update.json when I try to run my next.js application Below is the content of my next.config.js file: const isUnsafeEval = process.env.NODE_ENV == ...

Increment the name field automatically and track the number of auto-increment variables being sent through serialization

I am trying to implement a functionality where users can add more inputs by pressing a button, each representing an additional 'guest'. The issue I am facing is with auto-incrementing the JavaScript so that new inputs are added with an incremente ...

Is there a way to transfer JavaScript data to PHP?

<div> <p>This is a sample HTML code with JavaScript for tallying radio button values and passing them to PHP via email.</p> </div> If you need help converting JavaScript data to PHP and sending it via email, there are v ...