0

Following is my project structure.

test-proj
   |_ src
     |_main
     |   |_java
     |_test
         |_java
   |_prop.properties
   |_pom.xml

And my pom.xml

<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>com.sample</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <!-- The resources tag will be used if prop file is under src location. -->
    <!--    <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources> -->
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>properties-maven-plugin</artifactId>
                <version>1.0.0</version>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>read-project-properties</goal>
                        </goals>
                        <configuration>
                            <files>
                                <file>${basedir}/prop.properties</file> 
                            </files>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring-version}</version>
        </dependency>
    </dependencies>
</project>

Prop.properties file has

spring-version=3.1.0.RELEASE

I have tried to use maven-read properties plugin to read the properties and substitute the same in the place of spring version. But it throws up error saying 'dependencies.dependency.version' for org.springframework:spring-beans:jar must be a valid version but is '${spring-version}'.

I tried to use validate phase instead of initialize in the maven execution phase. But still the problem persists. Instead of context path, i tried replacing the property file location to be an absolute path D:\test-proj\prop.properties in configuration which didnt help me either. I am using maven compiler plugin version 2.3.2. Am I missing something? Please let me know if its feasible to substitute dependency versions by some other plugin as well.

Note: I will not be able to use parent-child pom relationship as all my projects are modular and they don't depend on the same parent

Poppy
  • 2,902
  • 14
  • 51
  • 75
  • Is your properties really on the basedir folder? basedir represents the root folder of the project, i.e, where the pom.xml file resides. I think your properties is probably on the resource folder, right? – dambros Apr 11 '16 at 07:53
  • @dambros I tried placing it in root directory as well as resources. As you could see, the current location of prop.properties is root folder where pom.xml is available and I have referred it as ${basedir.properties}/prop.properties – Poppy Apr 11 '16 at 07:59
  • Any particular reason not to declare the dependencies versions inside the pom itself? Just trying to understand the use of the plugin here, because it seems overkill – dambros Apr 11 '16 at 08:03
  • @dambros Most of my projects are independent but there are few properties in common like sql version, spring version etc. So I want to declare those common properties in a property file and use them where ever its needed. Parent-child wont work as my project structure is not designed that way. – Poppy Apr 11 '16 at 08:08
  • Note that this would make your build unreproducible. – kryger Apr 11 '16 at 08:13
  • This actually won't be possible. Check [this](http://stackoverflow.com/questions/9912632/maven-reading-a-property-from-an-external-properties-file). – dambros Apr 11 '16 at 08:16
  • Any other means by which I could achieve this ? :( – Poppy Apr 11 '16 at 08:23
  • 1
    @Poppy good point; tell us what do you want achieve. What is wrong when version is kept in pom.xml – michaldo Apr 11 '16 at 08:33
  • @michaldo I thought of defining the common properties in a property file and use them in pom files of different projects rather than defining the same property in all poms – Poppy Apr 11 '16 at 08:51

2 Answers2

0

If you want multiple unrelated project share dependecies version you can use Bill of Material concept http://howtodoinjava.com/maven/maven-bom-bill-of-materials-dependency/

  1. Create unrelated BOM project with packaging pom. Add shared dependencies to the project
  2. Each your project must import the BOM project

Below is snippet from the example page

How to add BOM [Bill Of Materials] dependency

Maven provides a tag dependencyManagement for this purpose. You need to add the bom information in this tag as follows. I am taking the example of Spring bom file.

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-framework-bom</artifactId>
      <version>4.0.1.RELEASE</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

An added benefit of using the BOM is that you no longer need to specify the version attribute when depending on Spring Framework artifacts. So it will work perfectly fine.

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
</dependency>
Community
  • 1
  • 1
michaldo
  • 4,195
  • 1
  • 39
  • 65
  • I'm afraid if i didnt get it correct. As per my understanding, the dependency management would ideally be defined in parent pom and the same dependency versions can be used in child poms inheriting the parent. In my case, the projects might not inherit same parent. – Poppy Apr 11 '16 at 09:48
  • @Poppy in my proposal dependency management section must be defined in each project independently But single entry (single BOM) may contain dozens dependencies to Spring, sql, apache-commons with versions) – michaldo Apr 11 '16 at 10:02
0

I cannot imagine that this is going to work. AFAIU Maven has to resolve dependencies prior to any build execution since how should it be able to create a proper classpath for executions otherwise?

Re:

I will not be able to use parent-child pom relationship as all my projects are modular and they don't depend on the same parent.

I think you are able. Establish a super-parent POM that contains appropriate setting(s) to be inherited by all and make all your projects' parents childs of this:

+- super-parent
  +- pom.xml ... containing setting(s) for all
  +- parent-project-1
     +- ...
  +- parent-project-2
     +- ...
  +- ...    
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107