1

I have a shell script that I want maven to source. I specifically want maven to source the file (source run.sh) rather than just executing it (./run.sh)

I have tried 2 different approaches with 2 different plugins and neither work:

<plugin> <artifactId>exec-maven-plugin</artifactId> <groupId>org.codehaus.mojo</groupId> <executions> <execution> <id>source file</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>source</executable> <arguments> <argument>./run.sh</argument> </arguments> </configuration> </execution> </executions> </plugin> 

and

<plugin> <artifactId>maven-antrun-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId> <executions> <execution> <id>source file</id> <phase>process-resources</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <exec executable="source"> <arg value="./run.sh"/> </exec> </target> </configuration> </execution> </executions> </plugin> 

They both fail saying that the command source does not exist

4
  • 1
    First I would ask why do you need that? Second sourcing only works on the current instance of the bash/shell... but using exec or alike will create everytime a new instance of shell/bash? Commented Apr 16, 2024 at 17:21
  • @khmarbaise The file I want to source sets environment variables and aliases that I need to reference in a subsequent command afterwards. I don't have control over this file b/c the commands inside it are constantly changing, so I need to source it in order to capture its environment variables dynamically. Commented Apr 16, 2024 at 17:38
  • That will not work, as I mentioned before during the exeuction of either exec-maven-plugin or maven-antrun-plugin the ./run.sh will create a new instance of shell/bash and after finished the ./run.sh it remove the instance... with it's environment variables... .. Why do you need aliases for a Maven build? You have to run that before the whole Maven build.... Commented Apr 16, 2024 at 19:28
  • @khmarbaise Thank you. I found that the best solution for me was to create a shell script that contains all the commands I wanted to execute. Then I can have a maven plugin execute my script once instead of trying to execute each command individually in a maven plugin. Commented Apr 17, 2024 at 12:13

2 Answers 2

0

I based my answer on @walyrious idea. I execute bash -c 'source run.sh; custom-script.sh' in maven-antrun-plugin so that custom-script.sh is in the same shell as the sourced run.sh. Though, I think this maven execution is much cleaner than his answer:

<plugin> <artifactId>maven-antrun-plugin</artifactId> <groupId>org.apache.maven.plugins</groupId> <executions> <execution> <id>source file</id> <phase>process-resources</phase> <goals> <goal>run</goal> </goals> <configuration> <target> <exec executable="bash"> <arg line="-c 'source run.sh; custom-script.sh'"/> </exec> </target> </configuration> </execution> </executions> </plugin> 
Sign up to request clarification or add additional context in comments.

Comments

0

Here's a solution that may be helpful at least as a workaround. TL;DR: The gist of this trick is to use the equivalent of bash -c "source foobar" instead of source foobar directly, since source is a shell builtin and not an executable file.

(Disclaimer: In my case I had to adapt a Maven project that was created by someone else, and was set up to use the exec-maven-plugin to install a Python dependency and run a Python script. I needed to run the commands within a virtual environment, and thus needed to run the typical source myenv/bin/activate command. The solution below is likely a kludge and not the ideal way to make this work)

Here's what the pom.xml had before (It assumed python3 and pip3 are already available):

</plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <id>install-python-dependencies</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>pip3</executable> <arguments> <argument>install</argument> <argument>-r</argument> <argument>path/to/requirements.txt</argument> </arguments> </configuration> </execution> <execution> <id>run-python-script</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>python3</executable> <arguments> <argument>path/to/custom-script.py</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> 

and here's how I adapted it to use source to set up a virtual environment:

</plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>3.4.1</version> <executions> <execution> <id>create-python-virtual-environment</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>python3</executable> <arguments> <argument>-m</argument> <argument>venv</argument> <argument>my_local_env</argument> </arguments> </configuration> </execution> <execution> <id>install-python-dependencies</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>bash</executable> <arguments> <argument>-c</argument> <argument>source my_local_env/bin/activate &amp;&amp; pip3 install -r path/to/requirements.txt</argument> </arguments> </configuration> </execution> <execution> <id>run-python-script</id> <phase>process-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <executable>python3</executable> <arguments> <argument>path/to/custom-script.py</argument> </arguments> </configuration> </execution> </executions> </plugin> </plugins> 

The changes done are as follows:

  1. A preliminary execution, named create-python-virtual-environment, was added at the start to, well, create a virtual environment
  2. The install-python-dependencies execution was modified to execute pip3 install within this virtual environment, which is activated via the source shell builtin

This second step required a few tricks to work:

  • The bash -c "source <some-script>" pattern was used to call source rather than have source be the value for the <executable> element directly (which fails, as you describe)
    • (If anyone's wondering, Python virtual environments must be activated via source mylocalenv/bin/activate; directly running the mylocalenv/bin/activate script does not work.)
  • The sourcing of the virtual environment and the pip3 install call were combined in the install-python-dependencies execution, since each execution happens in an isolated shell, and the virtual environment that is set up would no longer be active if the next command was executed separately.
    • Note: since this is a XML document, the && operator had to be escaped as &amp;&amp;

Again, I am NOT recommending this as a clean or desirable solution, but I'm sharing it here in case it helps out someone else as a quick workaround.

1 Comment

Im getting "java.io.IOException: Cannot run program "bash" (in directory "mypath\venv\Scripts"): CreateProcess error=2, The system cannot find the file specified ". How do you make bash available for the maven-exec-plugin?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.