Skip to content

Commit

Permalink
Make it possible to use the detected properties before reading pom.xml (
Browse files Browse the repository at this point in the history
#59)

Motivation:

It's currently not possible to use the detected properties to activate profiles.

Modifications:

PR implements afterSessionStart in addition to afterProjectsRead. This makes it possible to use the detected properties to activate profiles.

```
...
<profile>
  <id>Ubuntu</id>
  <activation>
    <property>
      <name>os.detected.release</name>
      <value>ubuntu</value>
    </property>
  </activation>
...
  <!-- Now you can do Ubuntu-specific work -->
...
</profile>
```

Result:

`os-maven-plugin` is now capable of injecting the detected properties even before reading `pom.xml`.
  • Loading branch information
nikolaybespalov authored Feb 20, 2023
1 parent fd2b70e commit 43ed4d4
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 3 deletions.
33 changes: 33 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.javadoc.failOnError>false</maven.javadoc.failOnError>

<!-- Using the same java.home for IT -->
<invoker.javaHome>${java.home}</invoker.javaHome>
</properties>

<dependencies>
Expand Down Expand Up @@ -190,6 +193,36 @@
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<debug>true</debug>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
<pomIncludes>
<pomInclude>*/pom.xml</pomInclude>
</pomIncludes>
<postBuildHookScript>verify</postBuildHookScript>
<!-- <settingsFile>src/it/settings.xml</settingsFile>-->
<streamLogs>true</streamLogs>
<goals>
<goal>clean</goal>
<goal>package</goal>
</goals>
</configuration>
<executions>
<execution>
<id>integration-test</id>
<phase>integration-test</phase>
<goals>
<goal>install</goal>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Expand Down
8 changes: 8 additions & 0 deletions src/it/test-activate-profile/.mvn/extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<extensions xmlns="http://maven.apache.org/EXTENSIONS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/EXTENSIONS/1.0.0 http://maven.apache.org/xsd/core-extensions-1.0.0.xsd">
<extension>
<groupId>@project.groupId@</groupId>
<artifactId>@project.artifactId@</artifactId>
<version>@project.version@</version>
</extension>
</extensions>
83 changes: 83 additions & 0 deletions src/it/test-activate-profile/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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>kr.motd.maven.it</groupId>
<artifactId>test-activate-profile</artifactId>
<version>1.0-SNAPSHOT</version>

<description>
An integration test that checks whether the profile can be activated by detected properties. If the
project.properties file contains the "active.profile" property and it has the expected value, the test is
considered successful.
</description>

<prerequisites>
<!-- (since Maven 3.3.1) configure your extension in .mvn/extensions.xml -->
<!-- https://maven.apache.org/examples/maven-3-lifecycle-extensions.html -->
<maven>3.3.1</maven>
</prerequisites>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<id>write-project-properties</id>
<phase>validate</phase>
<goals>
<goal>write-project-properties</goal>
</goals>
<configuration>
<outputFile>${project.build.directory}/project.properties</outputFile>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>detected-windows</id>
<activation>
<property>
<name>os.detected.name</name>
<value>windows</value>
</property>
</activation>
<properties>
<active.profile>detected-windows</active.profile>
</properties>
</profile>

<profile>
<id>detected-ubuntu</id>
<activation>
<property>
<name>os.detected.release</name>
<value>ubuntu</value>
</property>
</activation>
<properties>
<active.profile>detected-ubuntu</active.profile>
</properties>
</profile>

<profile>
<id>detected-osx</id>
<activation>
<property>
<name>os.detected.name</name>
<value>osx</value>
</property>
</activation>
<properties>
<active.profile>detected-osx</active.profile>
</properties>
</profile>
</profiles>
</project>
24 changes: 24 additions & 0 deletions src/it/test-activate-profile/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import org.apache.commons.io.FileUtils
import org.apache.commons.lang3.SystemUtils

def projectPropertiesFile = new File(basedir, "target/project.properties")

assert projectPropertiesFile.exists() : "project.properties not found"
assert projectPropertiesFile.canRead() : "project.properties cannot be read"

Properties projectProperties = new Properties()
projectPropertiesFile.withInputStream {
projectProperties.load(it)
}

if (SystemUtils.IS_OS_WINDOWS) {
assert projectProperties."active.profile" == 'detected-windows'
} else if (SystemUtils.IS_OS_LINUX) {
String osReleaseContent = FileUtils.readFileToString(new File("/etc/os-release"))

if (osReleaseContent.contains("ID=ubuntu")) {
assert projectProperties."active.profile" == 'detected-ubuntu'
}
} else if (SystemUtils.IS_OS_MAC) {
assert projectProperties."active.profile" == 'detected-osx'
}
21 changes: 18 additions & 3 deletions src/main/java/kr/motd/maven/os/DetectExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,17 @@ protected void logProperty(String name, String value) {
};
}

@Override
public void afterSessionStart(MavenSession session) throws MavenExecutionException {
injectProperties(session);
}

@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
injectProperties(session);
}

private void injectProperties(MavenSession session) throws MavenExecutionException {
// Detect the OS and CPU architecture.
final Properties sessionProps = new Properties();
sessionProps.putAll(session.getSystemProperties());
Expand All @@ -118,8 +127,10 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
injectSession(session, dict);

/// Perform the interpolation for the properties of all dependencies.
for (MavenProject p: session.getProjects()) {
interpolate(dict, p);
if (session.getProjects() != null) {
for (MavenProject p : session.getProjects()) {
interpolate(dict, p);
}
}
}

Expand All @@ -131,7 +142,11 @@ private static List<String> getClassifierWithLikes(MavenSession session) {
// Check to see if the project defined the
final Properties props = new Properties();
props.putAll(session.getUserProperties());
props.putAll(session.getCurrentProject().getProperties());

if (session.getCurrentProject() != null) {
props.putAll(session.getCurrentProject().getProperties());
}

return DetectMojo.getClassifierWithLikes(
props.getProperty(DetectMojo.CLASSIFIER_WITH_LIKES_PROPERTY));
}
Expand Down

0 comments on commit 43ed4d4

Please sign in to comment.