惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
博客园 - 叶小钗
A
About on SuperTechFans
Last Week in AI
Last Week in AI
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
MongoDB | Blog
MongoDB | Blog
T
The Blog of Author Tim Ferriss
Vercel News
Vercel News
博客园 - 司徒正美
博客园 - Franky
博客园 - 【当耐特】
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
大猫的无限游戏
大猫的无限游戏
博客园 - 聂微东
J
Java Code Geeks

Maciej Walkowiak - Java & Spring

Blog Generating HTTP clients in Spring Boot application from OpenAPI spec PostgreSQL and UUID as primary key Dynamic Projections with Spring Data JPA Container logs with Spring Boot and Testcontainers Reified Generics in Java? Faster integration tests with reusable Testcontainers and Flyway Running one-time jobs with Quartz and Spring Boot The best way to use Testcontainers with Spring Boot Spring Boot & Flyway - clear database between integration tests What's new in Spring? Spring Boot with Thymeleaf and Tailwind CSS - Complete Guide How to publish a Java library to Maven Central - Complete Guide Docker Compose - waiting until containers are ready Single file Java applications with JBang Beautiful bash scripts with Gum Running Java on CRaC How to log PostgreSQL queries with Testcontainers Spring Boot 3.0 & GraalVM Native Image - not a free lunch Creating Spring Cloud Function projects with AWS SAM Loading classpath resources to String with a custom JUnit extension Creating Project Templates with Cookiecutter Auto-Registering JUnit 5 extensions Spring Boot component scanning without annotations Listing Maven dependencies in Spring Boot Actuator Info endpoint Spring Cloud AWS 2.3 RC2 Released How I built vlad-cli - command line interface to Vlad Mihalcea The State of Java Relational Persistence On Choosing a Tech Stack
Activate Maven Profile by Operating System
2022-11-10 · via Maciej Walkowiak - Java & Spring
Published on
  • Maven
  • Java

Agent Smith from The Matrix

In rare cases, project build may require different configuration depending on operating system that runs the build.

In Maven, it can be done with Maven Profiles.

  • how to configure profiles per operating system
  • the trick to activate profile on Linux but not on Mac
  • how to enforce presence of active profile with maven-enforcer-plugin
  • how to run build on different operating systems with GitHub action

Configure Profiles ​

You can define profile for each supported operating system and provide different configuration, which includes dependencies, build plugins or just setting different OS specific property value.

xml

<profiles>
    <profile>
        <id>windows</id>
        <properties>
            <my.property>running.on.windows</my.property>
        </properties>
    </profile>
    <profile>
        <id>mac</id>
        <properties>
			<my.property>running.on.mac</my.property>
        </properties>
    </profile>
    <profile>
        <id>unix</id>
        <properties>
			<my.property>running.on.linux</my.property>
        </properties>
    </profile>
</profiles>

Profiles can be activated manually by setting -P flag to Maven command:

bash

$ mvn package -Pwindows

Likely, you would like to avoid the need for user to set this profile. Maven can figure it out by checking the operating system the build is running on and activating a profile for this OS:

xml

<profile>
    <id>windows</id>
	<activation>
		<os>
			<family>windows</family>
		</os>
	</activation>
    <properties>
        <my.property>running.on.windows</my.property>
    </properties>
</profile>

Activation can be narrowed to specific operating system name, architecture or even exact version. To not end up with dozens of profiles, assuming we are running builds only on x86 64bit architectures we can use family to let Maven activate the profile.

Family may have one of the following values:

  • dos
  • mac
  • netware
  • os/2
  • tandem
  • unix
  • windows
  • win9x
  • z/os
  • os/400
  • openvms

If you look closely, you may have noticed that there is no family for Linux. There is one for unix, but Mac is unix too.

Activate profile on Linux but not on Mac ​

When we use following profiles' config:

xml

<profiles>
    <profile>
        <id>windows</id>
        <activation>
            <os>
                <family>windows</family>
            </os>
        </activation>
        <properties>
			<my.property>running.on.windows</my.property>
        </properties>
    </profile>
    <profile>
        <id>mac</id>
        <activation>
            <os>
                <family>mac</family>
            </os>
        </activation>
        <properties>
			<my.property>running.on.mac</my.property>
        </properties>
    </profile>
    <profile>
        <id>linux</id>
        <activation>
            <os>
                <family>unix</family>
            </os>
        </activation>
        <properties>
			<my.property>running.on.linux</my.property>
        </properties>
    </profile>
</profiles>

Build will run as expected on Windows and Linux, but on Mac both mac and unix profile will be active.

To properly narrow it down to activate linux profile only when running on Linux, and mac profile only when running on Mac, the Linux profile must be activated by operating system name:

xml

<profiles>
    <profile>
        <id>windows</id>
        <activation>
            <os>
                <family>windows</family>
            </os>
        </activation>
        <properties>
			<my.property>running.on.windows</my.property>
        </properties>
    </profile>
    <profile>
        <id>mac</id>
        <activation>
            <os>
                <family>mac</family>
            </os>
        </activation>
        <properties>
			<my.property>running.on.mac</my.property>
        </properties>
    </profile>
    <profile>
        <id>linux</id>
        <activation>
            <os>
                <name>Linux</name>
            </os>
        </activation>
        <properties>
			<my.property>running.on.linux</my.property>
        </properties>
    </profile>
</profiles>

How to prevent from running on different OS or on unsupported architecture? ​

With such configuration, when build runs on the OS that does not match any of the activations, simply no profile will be active and likely build fails in unexpected way due to a missing whatever-you-defined-in-profile-configuration.

To avoid it, you can use maven-enforcer-plugin to ensure that one of the profiles is active:

xml

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-enforcer-plugin</artifactId>
            <version>3.1.0</version>
            <executions>
                <execution>
                    <id>enforce-os-profile</id>
                    <goals>
                        <goal>enforce</goal>
                    </goals>
                    <configuration>
                        <rules>
                            <requireActiveProfile>
                                <profiles>windows,mac,linux</profiles>
                                <all>false</all>
                            </requireActiveProfile>
                        </rules>
                        <fail>true</fail>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Run build on different operating systems with GitHub action ​

If your build depends on the operating system, it is worth to run it on the CI server against each of the supported OS.

GitHub Actions supports running jobs on Ubuntu, Windows and Mac OS. A sample action that runs Maven build on each of these is:

yml

name: Build
on: [push]
jobs:
  build:
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '17'
          cache: maven
      - run: ./mvnw package

Unfortunately at this stage there is no out-of-the-box support for running jobs on runners with ARM processors, such as Apple M1. If that's what you need, you must use a self hosted runner on GitHub and one of the providers that offer M1 - but it is not free.

Let's stay in touch and follow me on Twitter: @maciejwalkowiak

Subscribe to RSS feed