Installing node once only and not each time with Maven build - maven

I need one help .
I am using grunt for compiling CSS/JS
I have an issue that my node package is getting created with each build and it is taking a lot of space in Jenkins . I am using maven front end plugin for the same .
I want that node package gets only created once initially and not again with each maven build .
<id>grunt</id>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>0.0.26</version>
<!-- optional -->
<configuration>
<workingDirectory>DIR
</workingDirectory>
<nodeVersion>v4.2.1</nodeVersion>
<npmVersion>3.5.1</npmVersion>
<installDirectory>node</installDirectory>
</configuration>
<executions>
<execution>
<id>node and npm install</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
<installDirectory>node</installDirectory>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
<installDirectory>node</installDirectory>
</configuration>
</execution>
<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
We are doing a build with maven -P grunt . It is creating and installing node with each maven build .
For having node globally I am trying maven -P grunt -g , but it is not working .
In the GitHub group , I saw people mentioning we can't do with maven frontend plugin , so I tried maven exec plugin .
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<id>prepare-dist-npm-runtime-dependency</id>
<phase>prepare-package</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>node/node</executable>
<workingDirectory>dist</workingDirectory>
<arguments>
<argument>../node/npm/cli.js</argument>
<argument>install</argument>
<argument>--production</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
But I am not able to see it working . Can anyone help how to run maven to get it working for global node installation and not installing node with each build ?
If any other suggestion to have node installed only once and not globally will be grateful .

You can't install Node or NPM globally with the Frontend maven plugin. If we take a look at the documentation we'll see that the entire purpose of the plugin is to be able to install Node and NPM in an isolated environment solely for the build and which does not affect the rest of the machine.
Node/npm will only be "installed" locally to your project. It will not
be installed globally on the whole system (and it will not interfere
with any Node/npm installations already present.)
Not meant to replace the developer version of Node - frontend
developers will still install Node on their laptops, but backend
developers can run a clean build without even installing Node on their
computer.
Not meant to install Node for production uses. The Node usage is
intended as part of a frontend build, running common javascript tasks
such as minification, obfuscation, compression, packaging, testing
etc.
You can try to run the plugin with two different profiles, one for install and one for day-to-day use after installation. In the below example, you should be able to install Node and NPM by running:
mvn clean install -PinstallNode
Then every build after:
mvn clean install -PnormalBuild
Or because normalBuild is set to active by default, just:
mvn clean install
Notice install goals are not in the day-to-day profile:
<profiles>
<profile>
<id>installNode</id>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>0.0.26</version>
<!-- optional -->
<configuration>
<workingDirectory>DIR</workingDirectory>
<nodeVersion>v4.2.1</nodeVersion>
<npmVersion>3.5.1</npmVersion>
<installDirectory>node</installDirectory>
</configuration>
<executions>
<execution>
<id>node and npm install</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
<installDirectory>node</installDirectory>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
<installDirectory>node</installDirectory>
</configuration>
</execution>
<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>normalBuild</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>0.0.26</version>
<!-- optional -->
<configuration>
<workingDirectory>DIR</workingDirectory>
<nodeVersion>v4.2.1</nodeVersion>
<npmVersion>3.5.1</npmVersion>
<installDirectory>node</installDirectory>
</configuration>
<executions>
<execution>
<id>grunt build</id>
<goals>
<goal>grunt</goal>
</goals>
<configuration>
<arguments>build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

Related

Spring boot layer jar and build pack

I just started using spring boot 2.3 with layer jar and build pack feature.
Docker image is always built when
mvn clean install/package
code is committed and requested PR in git
However, this will slow down build process, how can I control the phase in which the image is being built and how can I control if image should be built at all?
Following is configuration that added to pom file
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<layers>
<enabled>true</enabled>
</layers>
<image>
<name>${image.name}</name>
<env>
<BP_JVM_VERSION>${BP_JVM_VERSION}</BP_JVM_VERSION>
</env>
</image>
</configuration>
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
The build-image goal is attached to the package phase by default. It is run each time the package goal is run because of the executions configuration you have in your pom.xml:
<executions>
<execution>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
If you remove this <executions> block, build-image will not be run automatically, but can be run manually with mvn spring-boot:build-image.
Alternatively, you can attach the goal to a different phase like install by specifying the phase in the <execution> block like this:
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
You can use a spring-boot.build-image.skip property
Add it to the propertied with true value
<properties>
<spring-boot.build-image.skip>true</spring-boot.build-image.skip>
</properties>
so the build-image goal will be skipped by default. Whenever you want to build the image pass false to the cmd
mvn clean install -Dspring-boot.build-image.skip=false
Update:
If you want to change the phase from install to package, you need to configure the plugin as following:
<executions>
<execution>
<id>default</id>
<phase>none</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
<execution>
<id>build-image-during-package</id>
<phase>package</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>

How to create docker image in Spring Boot project

I have tried using spotify/docker-maven-plugin without any success .
Below is part of my pom.xml file
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>myrepo/maven-docker-spotify</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}-jar-with-dependencies.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
The spotify/docker-maven-plugin you are using is currently inactive. It's recommended using spotify/dockerfile-maven-plugin instead.
So change the plugin section of your pom.xml file to resemble below
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>spotify/foobar</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
Note: You can also try using JIB maven plugin that doesnt require you to have docker installed and works with minimal configuration. With JIB, Running below command in is enough to do the jo
mvn compile com.google.cloud.tools:jib-maven-plugin:0.9.2:dockerBuild

Robot framework and it's library how to put in POM to install on docker

Hi experts my use case is that I have to install robotframework and it's libraries on docker using POM.xml in maven style. because there are some limitations in my organization where we can not run chef on docker.
Here is I have created sample pom.xml. I need to do "pip install robotframework", pip install robotframework-databaselibrary" many more.
Can someone please help me out
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>robotframework</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<mkdir dir="${project.build.directory}/Library" />
<untar src="${project.build.directory}/Library/${robotframework.version}.tar.gz" compression="gzip" dest="${project.build.directory}/robotframework" />
</target>
</configuration>
</execution>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>0.0.2</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>pip install robotframework</executable>
</configuration>
</execution>
</executions>
</plugin>

Tagging created image with dockerfile-maven-plugin

I am using dockerfile-maven-plugin with the following configuration:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.3.6</version>
<executions>
<execution>
<id>build-image</id>
<goals>
<goal>build</goal>
</goals>
<configuration>
<tag>latest</tag>
<repository>root/${project.artifactId}</repository>
<buildArgs>
<APP_NAME>${project.artifactId}</APP_NAME>
</buildArgs>
</configuration>
</execution>
<execution>
<id>push-image</id>
<goals>
<goal>push</goal>
</goals>
<configuration>
<repository>${docker.registry.url}/root/${project.artifactId}</repository>
</configuration>
</execution>
</executions>
</plugin>
Project deployment fails due to:
[INFO] The push refers to a repository [my-repository:9090/root/image-name]
[ERROR] An image does not exist locally with the tag: my-repository:9090/root/image-name
[WARNING] An attempt failed, will retry 1 more times
org.apache.maven.plugin.MojoExecutionException: Could not push image
.
.
.
Prefixing the repository under the build goal (in similar way to push goal) solves the issue. But then the locally created image prefixed with the repository tag.
Didn't find any documentation reference on how to perform the tag before push task.
In other words, I want that my local docker images will contain 2 images after plugin execution:
REPOSITORY TAG IMAGE ID CREATED SIZE
root/image-name latest 7ac1144c607e 21 minutes ago 175MB
my-repository:9090/root/image-name latest 7ac1144c607e 21 minutes ago 175MB
My docker version is: 17.06.0-ce
We didn't want to build all over again even if existing docker layers would be used. In the first execution we build and push and in the second execution we just tag and push.
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-plugin.version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
<configuration>
<tag>${build.number}</tag>
</configuration>
</execution>
<execution>
<id>default-2</id>
<goals>
<goal>tag</goal>
<goal>push</goal>
</goals>
<configuration>
<tag>latest</tag>
</configuration>
</execution>
</executions>
<configuration>
<repository>${docker-remote.registry}/path/${project.version.lowercase}</repository>
<buildArgs>
<EAR_FILE>${project.build.finalName}.ear</EAR_FILE>
</buildArgs>
<useMavenSettingsForAuth>true</useMavenSettingsForAuth>
</configuration>
</plugin>
</plugins>
</build>
Adding additional execution step to my configuration solved the issue:
<execution>
<id>tag-image</id>
<phase>deploy</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<repository>${docker.registry.url}/root/${project.artifactId}</repository>
</configuration>
</execution>
You can also tag your image manually after a successful build with
mvn dockerfile:tag -Dproject.plugin.dockerfile.tag=my-repository:9090/root/image-name

Install Maven itself from Maven Central

I have a maven plugin I would like to test against different Maven versions (Ex.: 2.2.1 & 3.0.4). Ideally I don't want users running the build to have to install these exact versions manually.
Is it possible to install specific versions of Maven itself from Maven Central or some other source that would then cache them in the local Maven repo for subsequent builds?
Maven distributions are stored in Maven Central Repository, as you can see here:
http://mvnrepository.com/artifact/org.apache.maven/apache-maven
https://repository.sonatype.org/index.html#nexus-search;gav~org.apache.maven~apache-maven~~~~kw,versionexpand
Therefore, it can be used as a normal dependency with following coordinates:
tar.gz variant:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>apache-maven</artifactId>
<version>3.0.4</version>
<classifier>bin</classifier>
<type>tar.gz</type>
</dependency>
zip variant:
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>apache-maven</artifactId>
<version>3.0.4</version>
<classifier>bin</classifier>
<type>zip</type>
</dependency>
The rest is quite standard - you will probably use it in integration test poms, and call them with maven-invoker-plugin as recommended by #khmarbaise.
Why don't you simply just install a Continuous Integration (CI) server such as Jenkins / Hudson / TeamCity / etc? CI servers allow you to run your build against different versions of an SDK.
If your plugin is OSS (and on GitHub), I believe you can get free Jenkins hosting from Cloudbees.
Downloading Maven itself from Maven Central is not possible. You can only download it from their site.
You could do a thing like the following:
<profile>
<id>run-its</id>
<build>
<!-- Download the different Maven versions -->
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<id>download-maven-2.0.11</id>
<phase>prepare-package</phase>
<goals>
<goal>download-single</goal>
</goals>
<configuration>
<url>http://archive.apache.org/dist/maven/binaries/</url>
<fromFile>apache-maven-2.0.11-bin.tar.gz</fromFile>
<toDir>${project.build.directory}/maven/download/</toDir>
</configuration>
</execution>
<execution>
<id>download-maven-2.2.1</id>
<phase>prepare-package</phase>
<goals>
<goal>download-single</goal>
</goals>
<configuration>
<url>http://archive.apache.org/dist/maven/binaries/</url>
<fromFile>apache-maven-2.2.1-bin.tar.gz</fromFile>
<toDir>${project.build.directory}/maven/download/</toDir>
</configuration>
</execution>
<execution>
<id>download-maven-3.0.3</id>
<phase>prepare-package</phase>
<goals>
<goal>download-single</goal>
</goals>
<configuration>
<url>http://archive.apache.org/dist/maven/binaries/</url>
<fromFile>apache-maven-3.0.3-bin.tar.gz</fromFile>
<toDir>${project.build.directory}/maven/download/</toDir>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>truezip-maven-plugin</artifactId>
<version>1.0-beta-4</version>
<executions>
<execution>
<id>extract-maven-2.0.11</id>
<goals>
<goal>copy</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<fileset>
<directory>${project.build.directory}/maven/download/apache-maven-2.0.11-bin.tar.gz</directory>
<outputDirectory>${project.build.directory}/maven/tools/</outputDirectory>
</fileset>
</configuration>
</execution>
<execution>
<id>extract-maven-2.2.1</id>
<goals>
<goal>copy</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<fileset>
<directory>${project.build.directory}/maven/download/apache-maven-2.2.1-bin.tar.gz</directory>
<outputDirectory>${project.build.directory}/maven/tools/</outputDirectory>
</fileset>
</configuration>
</execution>
<execution>
<id>extract-maven-3.0.3</id>
<goals>
<goal>copy</goal>
</goals>
<phase>prepare-package</phase>
<configuration>
<fileset>
<directory>${project.build.directory}/maven/download/apache-maven-3.0.3-bin.tar.gz</directory>
<outputDirectory>${project.build.directory}/maven/tools/</outputDirectory>
</fileset>
</configuration>
</execution>
</executions>
</plugin>
<!--
This is currently needed due to a bug of the truezip-plugin cause it unpacks without permission!
see http://jira.codehaus.org/browse/MOJO-1796
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>chmod-files</id>
<phase>package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<chmod file="${project.build.directory}/maven/tools/apache-maven-2.0.11/bin/mvn" perm="+x"/>
<chmod file="${project.build.directory}/maven/tools/apache-maven-2.2.1/bin/mvn" perm="+x"/>
<chmod file="${project.build.directory}/maven/tools/apache-maven-3.0.3/bin/mvn" perm="+x"/>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>1.5</version>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>1.8.4</version>
</dependency>
</dependencies>
<configuration>
<debug>false</debug>
<!-- src/it-ip as for integration tests invoker plugin for the time of transition to maven-invoker-plugin -->
<projectsDirectory>src/it</projectsDirectory>
<showVersion>true</showVersion>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<preBuildHookScript>setup</preBuildHookScript>
<postBuildHookScript>verify</postBuildHookScript>
<settingsFile>src/it/settings.xml</settingsFile>
</configuration>
<executions>
<execution>
<id>integration-test-maven-2.0.11</id>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
<configuration>
<reportsDirectory>${project.build.directory}/invoker-reports-2.0.11</reportsDirectory>
<localRepositoryPath>${project.build.directory}/local-repo-2.0.11</localRepositoryPath>
<cloneProjectsTo>${project.build.directory}/it-2.0.11</cloneProjectsTo>
<mavenHome>${project.build.directory}/maven/tools/apache-maven-2.0.11</mavenHome>
<goals>
<goal>clean</goal>
<goal>test</goal>
</goals>
</configuration>
</execution>
<execution>
<id>integration-test-maven-2.2.1</id>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
<configuration>
<reportsDirectory>${project.build.directory}/invoker-reports-2.2.1</reportsDirectory>
<localRepositoryPath>${project.build.directory}/local-repo-2.2.1</localRepositoryPath>
<cloneProjectsTo>${project.build.directory}/it-2.2.1</cloneProjectsTo>
<mavenHome>${project.build.directory}/maven/tools/apache-maven-2.2.1</mavenHome>
<goals>
<goal>clean</goal>
<goal>test</goal>
</goals>
</configuration>
</execution>
<execution>
<id>integration-test-maven-3.0.3</id>
<goals>
<goal>install</goal>
<goal>run</goal>
</goals>
<configuration>
<reportsDirectory>${project.build.directory}/invoker-reports-3.0.3</reportsDirectory>
<localRepositoryPath>${project.build.directory}/local-repo-3.0.3</localRepositoryPath>
<cloneProjectsTo>${project.build.directory}/it-3.0.3</cloneProjectsTo>
<mavenHome>${project.build.directory}/maven/tools/apache-maven-3.0.3</mavenHome>
<goals>
<goal>clean</goal>
<goal>test</goal>
</goals>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
This will download the different Maven version unpack the .tar.gz archives and make mvn executable and use maven-invoker-plugin to run all integration test with these different maven versions.
BUT i can't recommend that. The better way is to use a CI solution (as already mentioned) which contains the different installations of Maven. Than you can run the integration tests for each Maven version separately.

Resources