Introduction
I'm currently working on a GitHub Action and I want to check if this work for many version of Java.
For this I want to update for each test the pom.xml file and change the java inside this.
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.pseudo</groupId>
<artifactId>template</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>template</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>JAVAVERSION</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
</properties>
</project>
Question
Does someone know how we can replace string in file for each test ?
CodePudding user response:
I found this GitHub Action: MathieuSoysal/replace-string-in-file
Maybe it corresponds to your issue, it works on Macos-latest, Ubunut-latest and Windows-latest.
Example of usage for your issue
name: Test your GitHub Actions
on: [push]
jobs:
test:
continue-on-error: true
strategy:
matrix:
java: [8, 11, 16, 17, 18, 19]
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
name: Test Java version: ${{ matrix.java }} - os: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Replace string in file
uses: MathieuSoysal/[email protected]
with:
file: pom.xml
old-string: JAVAVERSION
new-string: ${{ matrix.java }}
CodePudding user response:
You can use the Find and Replace GH Action to find and replace the version in your pom.xml
:
name: GitHub Action
on:
push:
branches: [ main ]
jobs:
build:
strategy:
matrix:
java: [ '8', '11', '17' ]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
name: Java ${{ matrix.Java }} sample
steps:
- uses: actions/checkout@v3
- name: Setup java
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: ${{ matrix.java }}
- name: Find and Replace
uses: jacobtomlinson/gha-find-replace@v2
with:
find: "JAVAVERSION"
replace: ${{ matrix.java }}
regex: false
include: 'pom.xml'
# the rest of needed steps
This WF will run a matrix
with different versions of Java and replace the JAVAVERSION
in the pom.xml
file with the specific version.
For this example, I also used the Setup Java GH Action.