Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add npm-login goal to authenticate in private npm repository #1021

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Last public release: [![Maven Central](https://maven-badges.herokuapp.com/maven-

## Changelog

### 1.12.1
Add npm-login goal to authenticate in private npm repository

### 1.12.1

* update Dependency: Jackson (2.13.0), Mockito (4.1.0), JUnit (5.8.1), Hamcrest (2.2; now a direct dependency)
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,40 @@ will help to separate your frontend and backend builds even more.
</execution>
```

### Running Npm Login

```xml
<execution>
<id>npm login</id>
<goals>
<goal>npm-login</goal>
</goals>

<!-- optional: the default phase is "generate-resources" -->
<phase>generate-resources</phase>

<configuration>
<npmRegistryURL>https://npm-registry.com</npmRegistryURL>
<npmRegistryServerId>npm-registry</npmRegistryServerId>
</configuration>
</execution>
```

and server section in ~/.m2/settings.xml
```xml
<?xml version="1.0"?>
<settings>
...
<servers>
<server>
<id>npm-registry</id>
<username>username</username>
<password>password</password>
</server>
</servers>
...
```

### Optional Configuration

#### Working directory
Expand Down Expand Up @@ -533,6 +567,7 @@ Tools and property to enable skipping
* jspm `-Dskip.jspm`
* karma `-Dskip.karma`
* webpack `-Dskip.webpack`
* npm-login `-Dskip.npm-login`

## Eclipse M2E support

Expand Down
169 changes: 169 additions & 0 deletions frontend-maven-plugin/src/it/npm-login/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.eirslett</groupId>
<artifactId>example</artifactId>
<version>0</version>
<packaging>pom</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-client-java</artifactId>
<version>5.11.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<executions>
<execution>
<id>npm-registry-mock-server-compile</id>
<phase>generate-resources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>generate random port</id>
<phase>generate-resources</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
<configuration>
<randomPort>true</randomPort>
<portNames>
<name>npm.registry.port</name>
</portNames>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>filter .npmrc zand verify.groovy</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${project.basedir}</directory>
<filtering>true</filtering>
<includes>
<include>verify.groovy</include>
<include>**/.npmrc</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-maven-plugin</artifactId>
<version>5.11.2</version>
<configuration>
<serverPort>${npm.registry.port}</serverPort>
<logLevel>INFO</logLevel>
<initializationClass>com.github.eirslett.maven.plugins.NpmRegistryMock</initializationClass>
</configuration>
<executions>
<execution>
<id>npm-registry-mock-server-start</id>
<phase>generate-resources</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>npm-registry-mock-server-stop</id>
<phase>verify</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>

<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
<version>@project.version@</version>

<configuration>
<installDirectory>target</installDirectory>
</configuration>

<executions>
<execution>
<id>npm login (empty)</id>
<goals>
<goal>npm-login</goal>
</goals>
<configuration>
<npmRegistryURL>http://localhost:${npm.registry.port}</npmRegistryURL>
<npmRegistryServerId>npm-registry</npmRegistryServerId>
<!--ONLY FOR IT TEST-->
<userHome>${project.basedir}/user.home/empty/</userHome>
</configuration>
</execution>

<execution>
<id>npm login (with_token)</id>
<goals>
<goal>npm-login</goal>
</goals>
<configuration>
<npmRegistryURL>http://localhost:${npm.registry.port}</npmRegistryURL>
<npmRegistryServerId>npm-registry</npmRegistryServerId>
<!--ONLY FOR IT TEST-->
<userHome>${project.basedir}/user.home/with_token/</userHome>
</configuration>
</execution>

<execution>
<id>npm login (without_token)</id>
<goals>
<goal>npm-login</goal>
</goals>
<configuration>
<npmRegistryURL>http://localhost:${npm.registry.port}</npmRegistryURL>
<npmRegistryServerId>npm-registry</npmRegistryServerId>
<!--ONLY FOR IT TEST-->
<userHome>${project.basedir}/user.home/without_token/</userHome>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.eirslett.maven.plugins;

import org.mockserver.client.MockServerClient;
import org.mockserver.client.initialize.PluginExpectationInitializer;

import static org.mockserver.model.Header.header;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.mockserver.model.HttpStatusCode.OK_200;

public class NpmRegistryMock implements PluginExpectationInitializer {
private static final String NPM_USERNAME = "username";
private static final String NPM_PASSWORD = "password";
private static final String NPM_TOKEN = "NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459";

private static final String HEADER_CONTENT_TYPE = "Content-Type";
private static final String MIME_TYPE_APPLICATION_JSON = "application/json; charset=UTF-8";

@Override
public void initializeExpectations(MockServerClient mockServerClient) {
mockServerClient.when(
request()
.withMethod("PUT")
.withPath("/-/user/org.couchdb.user:" + NPM_USERNAME)
.withHeader(HEADER_CONTENT_TYPE, MIME_TYPE_APPLICATION_JSON)
.withBody("{\"name\":\"" + NPM_USERNAME + "\",\"password\":\"" + NPM_PASSWORD + "\"}")
)
.respond(
response()
.withStatusCode(OK_200.code())
.withHeaders(
header(HEADER_CONTENT_TYPE, MIME_TYPE_APPLICATION_JSON)
)
.withBody("{\"rev\":\"_we_dont_use_revs_any_more\",\"id\":\"org.couchdb.user:undefined\"," +
"\"ok\":\"true\",\"token\":\"" + NPM_TOKEN + "\"}"));
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//localhost:${npm.registry.port}/:_authToken="NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459"
registry=https://localhost:${npm.registry.port}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//localhost/:_authToken="NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459"
registry=https://localhost
28 changes: 28 additions & 0 deletions frontend-maven-plugin/src/it/npm-login/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.nio.file.Paths

def buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
assertEmpty()
assertWithToken()
assertWithoutToken()

def assertEmpty() {
def npmrcFile = Paths.get(basedir.getAbsolutePath(), 'user.home', 'empty', '.npmrc').toFile()
assert npmrcFile.exists() : "${npmrcFile.getAbsolutePath()} does`exist";
assert npmrcFile.text.equals('//localhost:${npm.registry.port}/:_authToken="NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459"\n') : "incorrect content"
}

def assertWithToken() {
def buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('Token exists. Skipping execution.') : 'build was not successful'
def npmrcFile = Paths.get(basedir.getAbsolutePath(), 'user.home', 'with_token', '.npmrc').toFile()
assert npmrcFile.exists() : "${npmrcFile.getAbsolutePath()} does`exist";
assert npmrcFile.text.equals('//localhost:${npm.registry.port}/:_authToken="NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459"\nregistry=https://localhost:${npm.registry.port}\n') : "incorrect content"
}

def assertWithoutToken() {
def npmrcFile = Paths.get(basedir.getAbsolutePath(), 'user.home', 'without_token', '.npmrc').toFile()
assert npmrcFile.exists() : "${npmrcFile.getAbsolutePath()} does`exist";
assert npmrcFile.text.equals('//localhost:${npm.registry.port}/:_authToken="NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459"\n//localhost/:_authToken="NpmToken.22e3a730-9e62-11e8-98d0-529269fb1459"\n' +
'registry=https://localhost\n') : "incorrect content"
}
7 changes: 7 additions & 0 deletions frontend-maven-plugin/src/it/settings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
<?xml version="1.0"?>
<settings>
<servers>
<server>
<id>npm-registry</id>
<username>username</username>
<password>password</password>
</server>
</servers>
<mirrors>
<mirror>
<id>mrm-maven-plugin</id>
Expand Down
Loading