Skip to content

Commit

Permalink
[MINVOKER-365] Allow to pre-build script to set user properties
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed May 13, 2024
1 parent b8aed81 commit 89b9633
Show file tree
Hide file tree
Showing 13 changed files with 394 additions and 122 deletions.
65 changes: 65 additions & 0 deletions src/it/MINVOKER-365-user-poperties-from-pre-build/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.plugins.invoker.its</groupId>
<artifactId>minvoker-335</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>

<description>
Test to check a user properties generated by pre-build
</description>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.maven-enforcer-plugin>@version.maven-enforcer-plugin@</version.maven-enforcer-plugin>
<version.maven-clean-plugin>@version.maven-clean-plugin@</version.maven-clean-plugin>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>integration-test</id>
<phase>initialize</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<goals>
<goal>clean</goal>
<goal>initialize</goal>
</goals>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.apache.maven.plugins.invoker.its</groupId>
<artifactId>minvoker-365</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>@version.maven-clean-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>@version.maven-enforcer-plugin@</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<rules>
<requireFilesExist>
<files>
<file>${testFile}</file>
</files>
</requireFilesExist>
</rules>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

def logs = new File(basedir, 'build.log').text

assert logs.contains('[INFO] Clean is skipped.')
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.nio.file.Files

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

def userProperties = context.get('userProperties')

def testFilePath = Files.createTempFile('minvoker-365-prefix', 'suffix')
userProperties.put('testFile', testFilePath.toString())
userProperties.put('maven.clean.skip', 'true')
29 changes: 29 additions & 0 deletions src/it/MINVOKER-365-user-poperties-from-pre-build/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

def logs = new File(basedir, 'build.log').text

// check if properties are pass to execution
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("windows")) {
assert logs.contains("-D testFile=")
assert logs.contains("-D maven.clean.skip=true")
} else {
assert logs.contains("'-D' 'testFile=")
assert logs.contains("'-D' 'maven.clean.skip=true'")
}
Original file line number Diff line number Diff line change
Expand Up @@ -1838,24 +1838,15 @@ private boolean runBuild(
}

Map<String, Object> context = new LinkedHashMap<>();
Properties scriptUserProperties = new Properties();
context.put("userProperties", scriptUserProperties);

boolean selectorResult = true;
if (!runSelectorHook(basedir, context, logger)) {
return false;
}

try {
try {
scriptRunner.run("selector script", basedir, selectorScript, context, logger);
} catch (ScriptReturnException e) {
selectorResult = false;
return false;
} catch (ScriptException e) {
throw new RunFailureException(BuildJob.Result.ERROR, e);
}

try {
scriptRunner.run("pre-build script", basedir, preBuildHookScript, context, logger);
} catch (ScriptException e) {
throw new RunFailureException(BuildJob.Result.FAILURE_PRE_HOOK, e);
}
runPreBuildHook(basedir, context, logger);

for (int invocationIndex = 1; ; invocationIndex++) {
if (invocationIndex > 1 && !invokerProperties.isInvocationDefined(invocationIndex)) {
Expand Down Expand Up @@ -1889,6 +1880,7 @@ private boolean runBuild(

Properties userProperties =
getUserProperties(basedir, invokerProperties.getUserPropertiesFile(invocationIndex));
userProperties.putAll(scriptUserProperties);
request.setProperties(userProperties);

invokerProperties.configureInvocation(request, invocationIndex);
Expand All @@ -1911,12 +1903,8 @@ private boolean runBuild(
"Maven invocation failed. " + e.getMessage(), BuildJob.Result.FAILURE_BUILD);
}
}
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
} finally {
if (selectorResult) {
runPostBuildHook(basedir, context, logger);
}
runPostBuildHook(basedir, context, logger);
}
return true;
}
Expand All @@ -1931,6 +1919,31 @@ int getParallelThreadsCount() {
}
}

private boolean runSelectorHook(File basedir, Map<String, Object> context, FileLogger logger)
throws MojoExecutionException, RunFailureException {
try {
scriptRunner.run("selector script", basedir, selectorScript, context, logger);
} catch (ScriptReturnException e) {
return false;
} catch (ScriptException e) {
throw new RunFailureException(BuildJob.Result.ERROR, e);
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
return true;
}

private void runPreBuildHook(File basedir, Map<String, Object> context, FileLogger logger)
throws MojoExecutionException, RunFailureException {
try {
scriptRunner.run("pre-build script", basedir, preBuildHookScript, context, logger);
} catch (ScriptException e) {
throw new RunFailureException(BuildJob.Result.FAILURE_PRE_HOOK, e);
} catch (IOException e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}

private void runPostBuildHook(File basedir, Map<String, Object> context, FileLogger logger)
throws MojoExecutionException, RunFailureException {
try {
Expand Down
97 changes: 0 additions & 97 deletions src/site/apt/examples/post-build-script.apt.vm

This file was deleted.

Loading

0 comments on commit 89b9633

Please sign in to comment.