Passing Working Directory to npm in exec-maven-plugin - maven

I am trying to run npm as part of my maven build. I am using exec-maven-plugin and here is my plugin section from pom.xml
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>exec-npm-install</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>build</argument>
</arguments>
<workingDirectory>${basedir}/src/main/webapp</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
How ever when i run mvn clean package or mvn generate-sources npm is not running.
In my package.json i have the following scripts.
"scripts": {
"start": "npm run build && serve .",
"build": "npm run build-js && npm run build-css",
"watch": "npm run watch-js & npm run watch-css & serve .",
"watch-build": "npm run watch-js && npm run watch-css && npm run build-js && npm run build-css && serve .",
"test": "npm run lint -s && npm run build",
"build-css": "rework-npm index.css | cleancss -o build/build.css",
"build-js": "browserify --extension=.jsx --extension=.js src/app.jsx > build/build.js",
"watch-js": "watchify --extension=.jsx --extension=.js src/app.jsx -o build/build.js --debug --verbose",
"watch-css": "nodemon -e css --ignore build/build.css --exec 'rework-npm index.css -o build/build.css'",
"lint-eslint": "eslint .",
"lint-jscs": "jscs .",
"lint": "npm run lint-eslint && npm run lint-jscs"
}
And hence i am calling build.
Earlier when i used npm install i did see the npm packages being installed.
Also how do i specify the working directory for npm ??
I am passing using and that's where the package.json is located.
<workingDirectory>${basedir}/src/main/webapp</workingDirectory>
Thanks

You need to execute npm run build to run an npm script, but you are executing npm build, which is an npm lifecycle event.
<execution>
<id>npm run build</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>npm</executable>
<arguments>
<argument>run</argument>
<argument>build</argument>
</arguments>
</configuration>
</execution>

Related

--force option frontend maven plugin

Is there anyway how to use --force option for frontend maven plugin ?
[INFO] --- frontend-maven-plugin:1.12.1:npm (npm install) # justine ---
[INFO] npm ERR! code ERESOLVE
[INFO] npm ERR! ERESOLVE unable to resolve dependency tree
It should look like this :
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install --force</arguments>
<npmInheritsProxyConfigFromMaven>false</npmInheritsProxyConfigFromMaven>
</configuration>
</execution>

On Gitlab jacoco maven plugin not creating report or folder

I have a maven build with a jacoco plugin to measure coverage. This works locally and creates the report as expected. I then wanted to pull the report into gitlab and included the file as an artifact. This never worked though and using script command to list the directory I can see the the report folder has not been created.
from the pom.xml:
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<configuration>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>./coverage-reports/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
From gitlab-ci.yml
UnitTest:
stage: test
services:
- name: postgres:latest
alias: postgres
script:
- mvn clean test -DargLine="-Dspring.datasource.url=jdbc:postgresql://postgres:5432/$POSTGRES_DB -Dspring.datasource.password=$POSTGRES_PASSWORD" -DcodegenFile="$CODEGEN_FILE"
- pwd
- ls
- cd target
- ls
- cd coverage-reports
- ls
- cd jacoco-ut
- ls
only:
- merge_requests
output from the build:
$ pwd
/builds/BJSS/Sheffield-Engineering/carbon-adjusted-workload-scheduler
$ ls
Dockerfile
README.md
checkstyle-suppressions.xml
checkstyle.xml
docker-compose.yml
docker_postgres_init.sql
documentation
hooks
maven-settings.xml
mvnw
mvnw.cmd
pom.xml
rest
src
target
$ cd target
1291$ ls
checkstyle-cachefile
checkstyle-checker.xml
checkstyle-result.xml
checkstyle-suppressions.xml
classes
generated-sources
generated-test-sources
maven-status
surefire-reports
test-classes
this works on my local computer just not on gitlab, any ideas please ?
thanks

Output the frontend maven plugin results to a file

I have a frontend-maven-plugin in a super-pom which is used to build the npm artifacts. As part of this step, I decided to build a npm dependency tree and capture the results in a file.
I tried the below step which prints the dependency in StandardOut, is there a way to re-direct this to a file ?
<execution>
<id>npm tree</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>list --prod --json</arguments>
</configuration>
</execution>
Looked at the npm list, but it doesn't seem to have any file o/p option.
I stumbled across the same issue and finally worked out a solution.
Just add an entry unter the scripts-Tag in your package.json like this:
"scripts": {
"versions-out": "npm list -prod -json > versions.txt ",
"v2-out": "npm --version > npmv.txt ",
}
Then in your pom, just call the script with:
<execution>
<id>library versions</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run-script versions-out</arguments>
</configuration>
</execution>
I also ran the v2-out the same way, to ensure that this command uses the npm version specified in the pom rather then my local installation. Yes. it does!

Is there any shortcut/command available to skip karma tests? like -DskipTests for Maven

I have created Maven Web application and I generally use
mvn clean install
So it execute npm install command as its one of the execution goal and then it run karma tests
So my question is, is there any shortcut or command available to skip karma tests conditionally?
Like we have -DskipTests for Maven project to skip tests.
Off course we can simply remove the npm run test from the package.json file but I want to skip tests using command.
pom.xml
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<workingDirectory>web/app</workingDirectory>
<installDirectory>build</installDirectory>
<arguments>install
--registry=${npm.registry}
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Since I am using the frontend-maven-plugin I had to add below execution to plugin.
<executions>
<execution>
<id>javascript tests</id>
<goals>
<goal>karma</goal>
</goals>
<phase>test</phase>
</execution>
</executions>
After that, just using -DskipTest build argument able to skip karma tests.
mvn clean install -DskipTests
You can define a system property -Dmaven.test.skip=true to skip the unit test.
mvn package -Dmaven.test.skip=true
Define properties in pom.xml
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
Use -DskipTests in surefire plugin
mvn package -DskipTests
Define surefire plugin in pox.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
use
mvn clean install -DskipTests
-DskipTests will skip the Karma tests. Please refer to the following snippet available at https://github.com/eirslett/frontend-maven-plugin
"Skipping tests: If you run maven with the -DskipTests flag, karma tests will be skipped."
So, Front end test cases are getting executed under "frontend-maven-plugin" which seems to recognize only -DskipTests to skip test cases execution unlike "maven-surefire-plugin" which recognizes both "-DskipTests" as well as "-Dmaven.test.skip=true" command parameters. So, if we use -DskipTests rather -Dmaven.test.skip=true command parameters, both backend as well as front end test cases get skipped.
This is what has been tested locally using -DskipTests
Skip FE Tests

want to run exec-maven-plugin when spring-boot running

My Spring-boot project structure is below
-Project
- src
- main
- java
- resources
- webapp
- test
- frontend
- src
- static
- config
- package.json
- (after "npm install") node_modules
- (after "npm run build") dist
what I want to do is
when a project is installed, run "npm install"
when "spring-boot:run" runs, run "npm run build" and moves contents in front/dist to /main/webapp
here is my pom.xml
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>exec-npm-install</id>
<phase>generate-sources</phase>
<configuration>
<executable>npm</executable>
<workingDirectory>./frontend</workingDirectory>
<arguments>
<argument>install</argument>
</arguments>
</configuration>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
I don't know how can I trigger exec-npm-install exection
with Eclipse maven build, I tried generate-sources, spring-boot:run, install and compile, package but it didn't run.
and I want to know what command should I put, to move dist when "spring-boot:run"
Few things -
As pointed by #khmarbaise as well, you should move your <plugins> tree out of the <pluginManagement> and also use <version> for the plugins you are using.
I don't know how can I trigger exec-npm-install exection
Since the execution is tied to the generate-sources phase of the execution. If you try and execute mvn generate-sources from command line on the project directory, you shall get the execution triggered. Also executing mvn test, mvn install, mvn package should do the needful. Take a look at the Maven - Lifecycle Reference for this.
I want to know what command should I put, to move dist when
"spring-boot:run"
If you want to copy the resources to the /webapp directory, you shall take a look at maven-resources-plugin and change its phase accordingly.

Resources