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

Execute the following command to set up the NodeJS package on

Each time I attempt to install a package in my local directory with npm install connect, I keep receiving warnings about: "No such file or directory, open '/Users/felixfong/package.json'" However, I do not wish to install the package in my co ...

Tips for showcasing a date using date-fns tailored in a Mui DatePicker as Thursday, January 13th

I'm currently working on a project using CodeSandbox to format dates as dddd, MMMM Do. The expected output is Thursday, January 13th, but instead I'm receiving 0013, January 13th. According to the date-fns documentation found at Date-fns format, ...

"Exploring the Possibilities with WordPress and Waypoint Js

After exploring various resources and tutorials online, I am struggling to implement Waypoints with WordPress. Despite placing the necessary files like waypoints.min.js and waypoints.js in the designated download folder within the js directory of my templa ...

Combining two sets of elements in Java to form a Json using Jackson

Is there a way to combine two List of objects retrieved from the database into a single object in order to serialize with Jackson and deserialize in the view? ObjectMapper mapper = new ObjectMapper(); jsonTutorias = mapper.writeValueAsString(tuto ...

How Angular services transmit information to components

I have implemented a search field within my top-bar component and I am facing an issue in passing the input value of that search field to another component. Design Search service Top bar component Result component Operation Top bar component receives th ...

Utilize Vue.js 3 and InertiaJs to Retrieve Session Information in Laravel 9

I am currently working on my initial Laravel project, incorporating Vuejs for the frontend. One of the key features of my application is allowing a Super admin to log in as a User (impersonate). By clicking on the Impersonate Button, the word impersonate g ...

What has become of the main template or layout file in this SSR Vue server-side rendering webpack example?

The documentation for ssr vue (server side rendering for vuejs) includes an example of code structure that involves a webpack build step: Here is how the structure appears: src ├── components │ ├── Foo.vue │ ├── Bar.vue │ ...

Tips for extracting URL parameter values in React applications

Here is a component that allows for searching: import { ChangeEvent, useCallback, useState } from 'react'; export function SearchComponent() { const [searchValue, setSearchValue] = useState<string>(''); const updateSearchValu ...

Is there a way to organize a specific column based on user selection from a drop down menu on the client side?

When selecting Timestamp from the drop-down menu, I expect the TimeStamp to be sorted in either ascending or descending order. Similarly, if I choose Host, the Host should be sorted accordingly. Despite using the Tablesorter plugin, sorting doesn't s ...

Tips for validating the input type "password"

Below is the Jquery Script that I am using: $(document).ready(function(){ $("#myForm").validate({ rules: { username: { required:true }, password: { required:true ...

Converting JavaScript to JSON and saving dynamic properties

Having a basic knowledge in JavaScript, I am attempting to extract data from users and utilize it as JSON properties in the following format: <th colspan="2"><input id="dc_name" name='dc[name]'></input></th> $ ...

Monitoring Logfile in Ruby On Rails 3.1

Within my Ruby on Rails application, I have a set of scripts that need to be executed. In order to ensure they are working properly, the application must display and track the content of logfiles generated by these scripts. To provide more context: I util ...

The scrollIntoView function is not functioning as expected

Having an issue with my function called scrollIntoView. It just refuses to work :( Take a look at the code below: HTML <section class="page_mission"> <div class="page_mission_text"> <div class="scroll-animations"> <di ...

Exploring Protractor testing with Bootstrap modals

I'm having an issue testing a bootstrap modal. Here's the scenario: Click on a button to display the modal The modal body has a list of buttons When I click on one of them, it doesn't do anything My Protractor code snippet: element(by. ...

Embark on a journey through a preorder traversal of a Binary Tree using TypeScript

Hello! I've been tasked with creating a function that iterates over a binary tree and returns all its values in pre-order. Here is the code snippet: interface BinTree { root: number; left?: BinTree; right?: BinTree; }; const TreePreArray ...

Set the title attribute according to the content of the <p> tag

Recently, I encountered a situation where I had numerous p tags with a specific class (let's call it .text). My task was to include the title attribute containing the text of each tag. For example: <p> Hello, world </p> This would resul ...

How to Use JQuery to Display Elements with a Vague Name?

Several PHP-generated divs are structured as follows: <div style="width:215px;height:305px;background-color: rgba(255, 255, 255, 0.5);background-position: 0px 0px;background-repeat: no-repeat;background-size: 215px 305px;display:none;position:fixed;top ...

Show the button only if the checkbox is selected

Can someone help me figure out how to show a button on the header bar once the checkbox is selected in a listview? Any assistance would be greatly appreciated. I have tried implementing this on my Codepen: `http://codepen.io/Hin/pen/KpGJZX` ...

Error 600: The element you have selected is not a valid target for this action. (Internet Explorer exclusive

I have been developing an AJAX calendar that functions perfectly in Chrome, Safari, and Firefox. However, I am encountering issues with Internet Explorer 9 and earlier versions. The error message I am receiving is SCRIPT 600: Invalid target element for th ...

How can I incorporate JSON data retrieved from my backend server into the content of Tabs in a ReactJS application, utilizing either the map() or forEach() method

I need help assigning a key from an object to each tab using a loop in order to navigate to its content when clicked on. I attempted to use map() but it didn't work, so I'm looking for guidance as I am new to React. Below is the code. Any assist ...