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

Support environment file next to environment variables #1156

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example",
"version": "0.0.1",
"scripts": {
"test": "node script.mjs"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>

<build>
<plugins>
<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>
<environmentFile>${project.basedir}/.env</environmentFile>
</configuration>

<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v16.17.1</nodeVersion>
</configuration>
</execution>

<execution>
<id>npm test</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>test</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log(`ENVIRONMENT_VARIABLE_FILE_1: ${process.env.ENVIRONMENT_VARIABLE_FILE_1}`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
assert new File(basedir, 'target/node').exists() : "Node was not installed in the custom install directory";
assert new File(basedir, 'target/node/npm').exists() : "npm was not copied to the node directory";

import org.codehaus.plexus.util.FileUtils;

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'));

assert buildLog.contains("File containing environment variables (configuration 'environmentFile') at '${basedir}/.env' could not be found, skipping it." as CharSequence) : 'environmentFile not missing'
assert buildLog.contains('ENVIRONMENT_VARIABLE_FILE_1: undefined') : 'environmentFile should not working'
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ENVIRONMENT_VARIABLE_FILE_1=1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example",
"version": "0.0.1",
"scripts": {
"test": "node script.mjs"
}
}
47 changes: 47 additions & 0 deletions frontend-maven-plugin/src/it/environment-variables-file/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>

<build>
<plugins>
<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>
<environmentFile>${project.basedir}/.env</environmentFile>
</configuration>

<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v16.17.1</nodeVersion>
</configuration>
</execution>

<execution>
<id>npm test</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>test</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log(`ENVIRONMENT_VARIABLE_FILE_1: ${process.env.ENVIRONMENT_VARIABLE_FILE_1}`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
assert new File(basedir, 'target/node').exists() : "Node was not installed in the custom install directory";
assert new File(basedir, 'target/node/npm').exists() : "npm was not copied to the node directory";

import org.codehaus.plexus.util.FileUtils;

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'));

assert buildLog.contains('ENVIRONMENT_VARIABLE_FILE_1: 1') : 'environmentFile not working'
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
2 changes: 2 additions & 0 deletions frontend-maven-plugin/src/it/environment-variables-mixed/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ENVIRONMENT_VARIABLE_FILE_1=1
ENVIRONMENT_VARIABLE_OVERRIDE=2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example",
"version": "0.0.1",
"scripts": {
"test": "node script.mjs"
}
}
51 changes: 51 additions & 0 deletions frontend-maven-plugin/src/it/environment-variables-mixed/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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>

<build>
<plugins>
<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>
<environmentFile>${project.basedir}/.env</environmentFile>
<environmentVariables>
<ENVIRONMENT_VARIABLE_OVERRIDE>3</ENVIRONMENT_VARIABLE_OVERRIDE>
<ENVIRONMENT_VARIABLE_4>4</ENVIRONMENT_VARIABLE_4>
</environmentVariables>
</configuration>

<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v16.17.1</nodeVersion>
</configuration>
</execution>

<execution>
<id>npm test</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>test</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

console.log(`ENVIRONMENT_VARIABLE_FILE_1: ${process.env.ENVIRONMENT_VARIABLE_FILE_1}`)
console.log(`ENVIRONMENT_VARIABLE_OVERRIDE: ${process.env.ENVIRONMENT_VARIABLE_OVERRIDE}`)
console.log(`ENVIRONMENT_VARIABLE_4: ${process.env.ENVIRONMENT_VARIABLE_4}`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
assert new File(basedir, 'target/node').exists() : "Node was not installed in the custom install directory";
assert new File(basedir, 'target/node/npm').exists() : "npm was not copied to the node directory";

import org.codehaus.plexus.util.FileUtils;

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'));

assert buildLog.contains('ENVIRONMENT_VARIABLE_FILE_1: 1') : 'environmentFile not working'
assert buildLog.contains('ENVIRONMENT_VARIABLE_OVERRIDE: 3') : 'environmentVariables not working overriding environmentFile'
assert buildLog.contains('ENVIRONMENT_VARIABLE_4: 4') : 'environmentVariables not working'
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example",
"version": "0.0.1",
"scripts": {
"test": "node script.mjs"
}
}
49 changes: 49 additions & 0 deletions frontend-maven-plugin/src/it/environment-variables/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?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>

<build>
<plugins>
<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>
<environmentVariables>
<ENVIRONMENT_VARIABLE_1>1</ENVIRONMENT_VARIABLE_1>
</environmentVariables>
</configuration>

<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v16.17.1</nodeVersion>
</configuration>
</execution>

<execution>
<id>npm test</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>test</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3 changes: 3 additions & 0 deletions frontend-maven-plugin/src/it/environment-variables/script.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'use strict';

console.log(`ENVIRONMENT_VARIABLE_1: ${process.env.ENVIRONMENT_VARIABLE_1}`)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
assert new File(basedir, 'target/node').exists() : "Node was not installed in the custom install directory";
assert new File(basedir, 'target/node/npm').exists() : "npm was not copied to the node directory";

import org.codehaus.plexus.util.FileUtils;

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'));

assert buildLog.contains('ENVIRONMENT_VARIABLE_1: 1') : 'environmentVariables not working'
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import java.util.Properties;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -14,6 +19,7 @@
import com.github.eirslett.maven.plugins.frontend.lib.FrontendException;
import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException;
import com.github.eirslett.maven.plugins.frontend.lib.PreExecutionException;

public abstract class AbstractFrontendMojo extends AbstractMojo {

Expand Down Expand Up @@ -48,7 +54,17 @@ public abstract class AbstractFrontendMojo extends AbstractMojo {
protected File installDirectory;

/**
* Additional environment variables to pass to the build.
* File containing environment variables to be passed to the build. Environment variables passed via the
* {@link AbstractFrontendMojo#environmentVariables} will override the ones present inside the file.
*
* @since 1.16
*/
@Parameter(property = "environmentFile", required = false)
protected File environmentFile;

/**
* Additional environment variables to pass to the build. If used alongside {@link AbstractFrontendMojo#environmentFile} then
* environment variables here will override the ones present inside the file.
*/
@Parameter
protected Map<String, String> environmentVariables;
Expand Down Expand Up @@ -107,4 +123,40 @@ public void execute() throws MojoFailureException {
}
}

/**
* Computes the environment variables based on the configuration provided. It will first evaluate the
* {@link AbstractFrontendMojo#environmentFile} configuration and then the {@link AbstractFrontendMojo#environmentVariables}. In case
* the latter one contains environment variables also present in the file, they will be overwritten.
* In case the {@link AbstractFrontendMojo#environmentFile} configuration is done but the file cannot be found, an error is printed but
* the build will still continue!
*
* @return the aggregated environment variables, may be empty
* @throws PreExecutionException when working with the environment file an exception occurs
*/
protected Map<String, String> getEnvironmentVariables() throws PreExecutionException {
Map<String, String> variables = new HashMap<>();

if (environmentFile != null) {
try (FileInputStream is = new FileInputStream(environmentFile)) {
Properties prop = new Properties();
prop.load(is);

for (Object key : prop.keySet()) {
variables.put((String) key, prop.getProperty((String) key));
}
} catch (FileNotFoundException err) {
getLog().error("File containing environment variables (configuration 'environmentFile') at '"
+ environmentFile.getAbsolutePath() + "' could not be found, skipping it.");
} catch (IOException err) {
throw new PreExecutionException("Trying to read file containing environment variables (configuration 'environmentFile') at '"
+ environmentFile.getAbsolutePath() + "' failed.", err);
}
}

if (environmentVariables != null) {
variables.putAll(environmentVariables);
}

return variables;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.eirslett.maven.plugins.frontend.mojo;

import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.PreExecutionException;
import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException;
import org.apache.maven.execution.MavenSession;
Expand Down Expand Up @@ -42,9 +43,9 @@ protected boolean skipExecution() {
}

@Override
protected synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException {
protected synchronized void execute(FrontendPluginFactory factory) throws TaskRunnerException, PreExecutionException {
ProxyConfig proxyConfig = getProxyConfig();
factory.getBowerRunner(proxyConfig).execute(arguments, environmentVariables);
factory.getBowerRunner(proxyConfig).execute(arguments, getEnvironmentVariables());
}

private ProxyConfig getProxyConfig() {
Expand Down
Loading