Lainezor
Lainezor

Reputation: 500

Dynamically change maven dependencies

I have a POM file with one dependency on Freemarker.jar. In the library folder there are several versions of the freemarker jar. I am wondering if there is an easier way to update which freemarker jar is being used without having to open the pom and change the name of the jar or having to find the jar and rename it manually. A JComboBox with the different freemarker jars would be the best but I have no idea how to make it change during runtime. I would be fine with having to restart the application as long as all I have to do is change the selection of the combobox and restart.

I have read a few similar questions and I believe it might not be possible.

Here's my dependency:

<dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
        <version>2.3.19</version>
    </dependency>

Upvotes: 1

Views: 4800

Answers (2)

Ren&#233; Link
Ren&#233; Link

Reputation: 51483

You can use the exec-maven-plugin to start the application together with a dependency management in maven. The version of the freemarker dependency must be overridable by the command line. For that you can use maven properties.

Then your user can restart the application with a different freemarker version by choosing it through a command line parameter.

For example something like this:

mvn exec:java -D=freemarker.version=2.3.19

But there are 3 limitiations:

  1. Your users need to restart the application
  2. This solution is only possible if the freemarker versions are binary compatible
  3. If the freemarker versions are only source compatible, your users additionally need to re-compile the application before starting it.

If you try this solution you should begin with 2 freemarker version that are very close, e.g. 2.3.19. 2.3.18 and try if they are compatible.

Step 1: Add the freemarker dependency to the dependency management.

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>${freemarker.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

Step 2 Add a default version property for the case that the user does not specify one at the command line.

<properties>
    <freemarker.version>2.3.19</freemarker.version>
</properties>

Step 3 Configure the exec-maven-plugin

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.2.1</version>
  <configuration>
    <mainClass>org.your.full.quallified.MainClass/mainClass>
  </configuration>
</plugin>

Step 4 Try to execute it with the default freemarker version

mvn exec:java

Step 5 Try to execute it with another freemarker version

mvn exec:java -D=freemarker.version=2.3.18

Upvotes: 2

Andres Olarte
Andres Olarte

Reputation: 4380

I don't think you could use maven for this, since maven is (normally) not used during runtime, only during compile/build. You could change the scope of your dependency to "provided", and then tweak the mechanism you're using to start your application, to add the correct jar to your classpath. However, with more details on how you run your application, it's hard to give more details.

EDIT: changed to the correct scope.

Upvotes: 0

Related Questions