Output the frontend maven plugin results to a file - maven

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!

Related

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.

Watch SCSS with a Spring-boot application

I have a Spring-boot application, in which I'd like to use SCSS to improve my CSS. I have successfully configured my pom for my SCSSfile to be compiled into a valid CSS during maven compilation. I now would like to have a hot deploy of the SCSS. Any time the SCSS is changed, I'd like it to be recompiled and redeployed in my launched spring-boot app, so that I just have to refresh my browser to see the changes.
My goal is just having to execute the maven command:
mvn install spring-boot:run sass:watch
for it to work. Is it possible?
I have not yet managed to make the sass:watch work when run from another terminal, but I assume this is just a pom configuration issue.
EDIT: My application is built as a fat (or uber) jar
Because I had the same issue:
You cannot run spring-boot:run sass:watch as a single command.
Maven is not (really) multi-threaded and the tasks are executed as a sequence. Since both tasks are blocking, the second one will never be executed.
So you need to run two commands from two consoles
mvn spring-boot:run
mvn sass:watch
the mvn install is not really needed. If you still want it, put it in the first command.
You can use the exec-maven-plugin to execute the sass:watch maven goal asynchronously:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>mvn</executable>
<async>true</async>
<arguments>
<argument>sass:watch</argument>
</arguments>
</configuration>
</plugin>
To execute:
mvn exec:exec spring-boot:run
Or you can bind the exec goal to a particular phase (e.g. validate) and/or wrap this in a profile so it only executes when you want it:
mvn spring-boot:run -P sass
<profile>
<id>sass</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<executable>mvn</executable>
<async>true</async>
<arguments>
<argument>sass:watch</argument>
</arguments>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>

How to change maven task order?

I'm trying to use lesscss-mvn this plugin to compile all less into css.
But the thing is, before that I need to copy all the less files from the other folder.
However, it seems in maven ,it's execute less compile first. How can I to change maven task order?
Here is less-css xml:
...Clean task, remove all files in target/app1/
... Here is to copy all less files from another folder to ${project.basedir}/target/app1/styles/less
...
<plugin>
<groupId>org.lesscss</groupId>
<artifactId>lesscss-maven-plugin</artifactId>
<version>1.7.0.1.1</version>
<configuration>
<sourceDirectory>${project.basedir}/target/app1/styles/less</sourceDirectory>
<outputDirectory>${project.basedir}/target/app1/styles/css</outputDirectory>
<compress>true</compress>
<includes>
<include>main.less</include>
</includes>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
When I run this, err message is some error like:
C:/project/MyApp/target/app1/styles/less is not existed.
I am so sure this is because maven run compile less task first. (Clean all files first, and compile less. There is no files in target/ now. So maven throw an error)
How to make maven run copy task first?
Thx for your help.
Goal cannot be changed, but you can change phase when the goal compile is executed:
<execution>
<phase>process-test-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
Available phases: http://maven.apache.org/ref/3.2.3/maven-core/lifecycles.html

Can't execute a maven shell script with the prefix I gave it

I'm trying to make a post deploy script for my project, which will test validity.
Now, the script is made, and I'm trying to make it run after all the sub modules.
The simplest way I could think of was simply to call the exec-maven-plugin with a specific prefix. I need a new prefix though, since I already have a different exec running on one of the sub modules.
so, I want to call it as: validate:exec
the relevant part of the xml looks like this:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>validate.py</executable>
<goalPrefix>validate</goalPrefix>
<arguments>
<argument>${deploy.host.fe</argument>
<argument>${deploy.port.fe}/argument>
</arguments>
</configuration>
</plugin>
This I put in my pluginsmanagement or plugins tag (spelled correctly), neither helped.
when I try to run:
mvn validate:exec
I get:
No plugin found for prefix 'validate' in the current project and in the plugin groups...
Thanks in advance for any help

Better way to integrate maven/qunit/phantomjs?

I have been investigating the best way to do JS unit testing in our maven CI environment. What I currently have cobbled together is the following in my maven project:
qunit resources (JS/CSS files)
qunit test html files (one for each file under test) with html fixture if required
index html file which references the test html files as an ordered list of hyperlinks
PhantomJS runner file, which:
opens the index html file and parses out list of test files
opens each test file
takes a screenshot of the qunit test results for each file
If there are any failures, exit with a status of "1"
If there are no failures, exit with a status of "0"
shell file which will exit with "0" if phantomjs isn't installed, will call the phantomjs tests if it is installed
changes to pom.xml to run phantomjs tests during test phase of build:
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1</version>
<executions>
<execution>
<id>PhantomJS Unit Testing</id>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>${project.basedir}/src/main/webapp/unittest/phantomcheck</executable>
<arguments>
<argument>${project.basedir}/src/main/webapp/unittest/qunit-runner.js</argument>
<argument>${project.basedir}/src/main/webapp/unittest/tests/index.html</argument>
<argument>${project.build.directory}/surefire-reports</argument>
</arguments>
</configuration>
</plugin>
</plugins>
So, this works nicely. It runs the qunit tests during builds on our dev and build machines (as long as PhantomJS is installed). The tests run in a headless browser environment with no restrictions on the qunit tests. Other maven/qunit integrations I've seen fall short due to running the tests in Rhino, or other JS environments which place restrictions on the type of tests we can write. Plus phantomjs gives us the ability to have the screenshots of the test runs, which are helpful in troubleshooting any failures.
The drawback to my approach is that a PhantomJS installation is required on the build/dev machine. I don't know how to bundle phantomJS into a dependency such that developers don't need to worry about installing PhantomJS. Can anyone give me a push in this direction? How do I get started?
The phantomjs-maven-plugin provides an install goal for installing phantomjs so you don't need it pre-installed. After it installs phantomjs it sets a property with the path to the executable that other plugins can then use. It also has an exec goal for executing phantomjs scripts. Full disclosure: I wrote the plugin.
Building on Kyle's answer I was able to find a solid solution to this issue. Thank you Kyle!
The solution is to use the phantomjs-maven-plugin Maven plugin. I add the plugin to my pom.xml like so (you will need to upgrade Maven to v3.1 or higher to use the plugin):
<plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>0.4</version>
<executions>
<execution>
<goals>
<goal>install</goal>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.7</version>
<checkSystemPath>false</checkSystemPath>
<script>src/test/qunit/run-qunit-testsuite.js</script>
<arguments>
<argument>src/test/qunit/testsuite.qunit.html</argument>
</arguments>
</configuration>
</plugin>
Important Caveat: in the pom.xml code above, make sure to use relative (not absolute) references to the files, as I've done. I wasted a few hours after using absolute references (starting at ${basedir}) only to find out that it does something strange to PhantomJS's working directory. Using relative references in your pom.xml will enable relative references inside your HTML file (which will maximize code portability).
In the plugin code above, I reference two files: run-qunit-testsuite.js and testsuite.qunit.html. The HTML file is just the QUnit file that executes all of your tests. The JS file is the driver for PhantomJS; it accepts one argument: the HTML QUnit test file to load.
To complete this solution, you can download sample driver and test files from GMarik's GitHub Gist page. You can and should adapt these files to your needs (although be aware that GMarik's page does not include an open source license, you will need to ask for permission for any copyright-infringing use).
When adding this plugin to your Maven code, after executing a Maven build you will see output like the following (adapted from GMarik's page):
[INFO] --- phantomjs-maven-plugin:0.4:exec (default) # project.name ---
[INFO] Executing phantomjs command
'waitFor()' finished in 200ms.
Tests completed in 21 milliseconds.
5 tests of 5 passed, 0 failed.
If the tests pass then your build will pass. If the tests fail then your build will fail!
Using Kyle's answer and another plugin I was able to get a full solution that doesn't require anything but maven preinstalled and sets up phantomjs and qunit to allow the running of tests. I started with a maven-grunt plugin (github.com/eirslett/frontend-maven-plugin) and followed the steps in this guide (http://blog.trifork.com/2014/10/07/setting-up-maven-to-use-gruntnodejs/) to get it set up. Then I tried to use qunit within maven and I ran into phantomjs trouble and came across this post and found out about Kyle's plugin (github.com/klieber/phantomjs-maven-plugin). I had to use a custom qunit source explained in this guide (http://techblog.dorogin.com/2013/08/issues-with-grunt-contrib-qunit.html). This allowed me to use kyles plugin to install phantomjs then link the binary through grunt options to the custom qunit. In the end my pom looked like:
` <plugin>
<groupId>com.github.klieber</groupId>
<artifactId>phantomjs-maven-plugin</artifactId>
<version>0.4</version>
<executions>
<execution>
<phase>generate-resources</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
<configuration>
<version>1.9.8</version>
</configuration>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>0.0.20</version>
<executions>
<execution>
<id>install node and npm</id>
<phase>generate-resources</phase>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v0.10.33</nodeVersion>
<npmVersion>1.3.6</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<phase>generate-resources</phase>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>grunt build</id>
<phase>generate-resources</phase>
<goals>
<goal>grunt</goal>
</goals>
<configuration>
<arguments>--phantomPath=${phantomjs.binary}</arguments>
</configuration>
</execution>
</executions>
</plugin>
`
My Gruntfile.js looked like:
` module.exports = function(grunt) {
grunt.loadNpmTasks('grunt-croc-qunit');
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
qunit: {
options: {
'phantomPath': grunt.option('phantomPath')
},
all:['src/test/*.html']
}
});
grunt.registerTask('default',['qunit']);
};`
And my package.json looked like:
` {
"name":"reporting",
"version":"0.0.1",
"dependencies": {
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13",
"grunt-croc-qunit":"~0.3.0"
},
"devDependencies":{ }
}`
We just check phantomJS.exe into source control. And then we are certain that the same version of phantomJS is being used on all machines.
This is an old question, but I thought I would link to a project of mine that uses PhantomJS and QUnit to run with TestNG:
The project is called qunit-testng. I also have a sample project that shows the library in use.
Here's a screenshot of test output:

Resources