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

Ensure maven.properties can be forwarded to system properties for exec:java #346

Merged
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
27 changes: 27 additions & 0 deletions src/main/java/org/codehaus/mojo/exec/AbstractProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.codehaus.mojo.exec;

/*
* 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.
*/

/**
* Base type for system properties.
*/
public class AbstractProperty
{
}
28 changes: 22 additions & 6 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;
import java.util.stream.Stream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.resolver.ArtifactResolutionException;
Expand Down Expand Up @@ -94,7 +95,7 @@ public class ExecJavaMojo
* @since 1.0
*/
@Parameter
private Property[] systemProperties;
private AbstractProperty[] systemProperties;

/**
* Indicates if mojo should be kept running after the mainclass terminates. Use full for server like apps with
Expand Down Expand Up @@ -542,14 +543,29 @@ private Collection<Thread> getActiveThreads( ThreadGroup threadGroup )
*/
private void setSystemProperties()
{
if ( systemProperties != null )
if ( systemProperties == null )
{
originalSystemProperties = System.getProperties();
for ( Property systemProperty : systemProperties )
return;
}
// copy otherwise the restore phase does nothing
originalSystemProperties = new Properties();
originalSystemProperties.putAll(System.getProperties());

if ( Stream.of( systemProperties ).anyMatch( it -> it instanceof ProjectProperties ) )
{
System.getProperties().putAll( project.getProperties() );
}

for ( AbstractProperty systemProperty : systemProperties )
{
if ( ! ( systemProperty instanceof Property ) )
{
String value = systemProperty.getValue();
System.setProperty( systemProperty.getKey(), value == null ? "" : value );
continue;
}

Property prop = (Property) systemProperty;
String value = prop.getValue();
System.setProperty( prop.getKey(), value == null ? "" : value );
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/codehaus/mojo/exec/ProjectProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.codehaus.mojo.exec;

/*
* 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.
*/

/**
* Represents all project properties (to set as system properties).
*/
public class ProjectProperties extends AbstractProperty
{
}
2 changes: 1 addition & 1 deletion src/main/java/org/codehaus/mojo/exec/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Kaare Nilsen ([email protected])
*/
public class Property
public class Property extends AbstractProperty
{
private String key;

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/codehaus/mojo/exec/SystemProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.codehaus.mojo.exec;

/*
* 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.
*/

/**
* Just to comply to maven configuration (object converter) and the legacy configuration.
*/
public class SystemProperty extends Property
{
}
71 changes: 71 additions & 0 deletions src/site/apt/examples/example-java-project-properties.apt.vm
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
------
Forward maven properties to exec:java goal
------
Romain Manni-Bucau
------
2022-11-14
------

~~ Copyright 2022 The Codehaus
~~
~~ Licensed 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.

~~ NOTE: For help with the syntax of this file, see:
~~ http://maven.apache.org/doxia/references/apt-format.html


You can ensure the maven properties are available to your main forcing `<projectProperties />` property type.

Tip: you can still override potentially conflicting properties if you set them *after*.

* pom.xml

-------------------
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>${project.version}</version>
<executions>
<execution>
...
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
...
</arguments>
<systemProperties>
<projectProperties />
<systemProperty>
<key>myproperty</key>
<value>myvalue</value>
</systemProperty>
...
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
-------------------

2 changes: 2 additions & 0 deletions src/site/apt/index.apt
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ Exec Maven Plugin
* {{{./examples/example-exec-using-toolchains.html} Using toolchains instead of explicit paths}}

* {{{./examples/example-exec-using-executabledependency.html} Using executable binary dependencies instead of local executables}}

* {{{./examples/example-java-project-properties.html} Forward maven system properties to the main}}
22 changes: 19 additions & 3 deletions src/test/java/org/codehaus/mojo/exec/ExecJavaMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,12 @@ public void testEmptySystemProperty()
{
File pom = new File( getBasedir(), "src/test/projects/project5/pom.xml" );

assertNull( "System property not yet created", System.getProperty( "project5.property.with.no.value" ) );
assertNull( "System property not yet created", System.getProperty( "test.name" ) );

execute( pom, "java" );
assertEquals( "Hello " + System.lineSeparator(), execute( pom, "java" ) );

assertEquals( "System property now empty", "", System.getProperty( "project5.property.with.no.value" ) );
// ensure we get back in the original state and didn't leak the execution config
assertNull( "System property not yet created", System.getProperty( "test.name" ) );
}

/**
Expand Down Expand Up @@ -275,6 +276,21 @@ public void testExcludedClasspathElement() throws Exception
}
}

/**
* Ensure all project properties can be forwarded to the execution as system properties.
*
* @throws Exception if any exception occurs
*/
public void testProjectProperties()
throws Exception
{
File pom = new File( getBasedir(), "src/test/projects/project18/pom.xml" );

String output = execute( pom, "java" );

assertEquals( "Hello project18 project" + System.lineSeparator(), output );
}

/**
* @return output from System.out during mojo execution
*/
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/codehaus/mojo/exec/HelloSystemProperty.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.codehaus.mojo.exec;

/**
* Simple class with a main method reading a system property
*/
public class HelloSystemProperty
{
/**
* Print on stdout hello and the system property test.name value.
*
* @param args the arguments
*/
public static void main( String... args )
{
// important: do not default on empty due to testEmptySystemProperty()
System.out.println( "Hello " + System.getProperty( "test.name" ) );
}
}
48 changes: 48 additions & 0 deletions src/test/projects/project18/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.cb.maven.plugins.exec</groupId>
<artifactId>project18</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Exec Plugin</name>
<inceptionYear>2005</inceptionYear>

<licenses>
<license>
<name>Apache License 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<test.name>${project.artifactId} project</test.name>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>org.codehaus.mojo.exec.HelloSystemProperty</mainClass>
<additionalClasspathElements>
<additionalClasspathElements>../../../../target/test-classes</additionalClasspathElements> <!-- test setup requires it -->
</additionalClasspathElements>
<systemProperties>
<projectProperties />
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>

</project>
4 changes: 2 additions & 2 deletions src/test/projects/project5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
</execution>
</executions>
<configuration>
<mainClass>org.codehaus.mojo.exec.DummyMain</mainClass>
<mainClass>org.codehaus.mojo.exec.HelloSystemProperty</mainClass>
<systemProperties>
<property>
<key>project5.property.with.no.value</key>
<key>test.name</key>
<value></value>
</property>
<property>
Expand Down