2

I have a case in which I have to write artifact name of one of project dependency in predefined xml file which will be keep with project jar and resides in resources folder of project. For example predefind.xml -

<project>
    <projectName>somename</projectName>
    <projectjar>
        <name>some.jar</name>
    </projectjar>
    <dependantJarsInfo>
        <folderName>dependant_jars</folderName>
        <dependantJars>
            <name>dependency.jar</name>
        </dependantJars>
    </dependantJarsInfo>    
</plugin> 

pom.xml

<project>
    <groupId>com.mycom</groupId>
    <artifactId>someproject</artifactId>
    <packaging>jar</packaging>
    <version>5.8.0.0</version>

    <dependencies>

        <dependency>
            <groupId>com.mycom.</groupId>
            <artifactId>dependency</artifactId>
            <version>LATEST</version>
        </dependency>

    </dependencies>

</project>

I need to get the full name like dependency-1.2.0 where LATEST=1.2.0, and write into given predefined.xml file at specified tag.

        <dependantJars>
            <name>dependency.jar</name>
        </dependantJars>

is this possible with maven if yes, then how ? and if no then what alternative should i take to resolve this issue.

Afgan
  • 1,022
  • 10
  • 32
  • What is the exact problem to write the version of the dependency into the pom ? And what are you trying to solve with this approach? – khmarbaise Aug 19 '16 at 17:43
  • khmarbaise thanks for response, we were push our build system in continues integration a fully automatic process in which using LATEST key word maven itself find the last build version of dependency, thats why we are not write verision of any in home dependency in pom, I hope you got my point. – Afgan Aug 20 '16 at 16:45

2 Answers2

1

Guys after doing research on this issue I got solution for this. I use two maven plugin to resolve it.

  1. maven-dependency-plugin
  2. gmaven-plugin

usage of this plugins -

 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.9</version>
<executions>
    <execution>
        <id>build-classpath</id>
        <phase>validate</phase>
        <goals>
            <goal>build-classpath</goal>
        </goals>
        <configuration>
        <outputFile>target/classPath.txt</outputFile>
            <includeArtifactIds>
                dependency
            </includeArtifactIds>
        </configuration>
    </execution>
</executions>
</plugin>   

<plugin>
    <groupId>org.codehaus.groovy.maven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <id>groovy-magic</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>

                <source>
                    def theInfoName = "target/classPath.txt";
                    File theInfoFile = new File(theInfoName)
                    def words
                    def key
                    def value
                    if (!theInfoFile.exists()) {
                    println "File does not exist"
                    } else {
                    theInfoFile.eachLine { line ->
                    if (line.trim().size() == 0) {
                    return null
                    } else {
                    words = line
                    File theInfoFileto = new File(words)
                    ant.replace(file: "target/classes/myPackagepath/predefind.xml", token: "dependency.jar", value: theInfoFileto.getName())
                    }
                    }
}                                
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>

Hope this will help some one.

Afgan
  • 1,022
  • 10
  • 32
-1

I think you actually have the wrong approach to your problem. You should try to use Maven Properties instead.

Example

You define the name of your page in your pom.xml :

<project>

  <properties>
    <package.name>dependency.jar</package.name>
  </properties>

</project>

Then, when you want to change the name of your package with command line, you can override it with :

mvn -Dpackage.name=othername package ...

But if you don't override the name of your package, you can still use the default value defined in your pom.xml

mvn package ...

Other SO question about this :

Community
  • 1
  • 1
Mickael
  • 4,458
  • 2
  • 28
  • 40
  • Mickaël B thanks for answer, but issue is not to take or update any thing in pom.xml, i just need to take dependency detail from maven at run time and inject into my predefined.xml file. – Afgan Aug 20 '16 at 16:49