2

I am trying to do two things here :
1. Copy the dependencies to two different folders :
a. package1 - contains my other project dependencies
b. package2 - contains third party jar dependencies
2. Write all the jars in both package1 and package2 to another file 'dependencyJars.txt'

The dependencyJars.txt must contain all the jars in package1 and package2 as well.

I am unable to achieve that.

The second build-classpath overwrites the dependencies written by first build-classpath.
So at the end, only package2 dependencies are available in dependencyJars.txt.
Is there any way to achieve the above thing, i.e, the first execution writes its dependencies to dependencyJars.txt, and then the second execution appends its dependencies in the file dependencyJars.txt?

My POM content is as follows :

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
    <execution>
        <id>build-package1-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>build-classpath</goal>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <appendOutput>true</appendOutput>
            <prefix>package1/</prefix>
            <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename>
            <excludeGroupIds>com.test.package2</excludeGroupIds>
            <outputDirectory>${project.build.directory}/package1-jars/</outputDirectory>
            <outputFile>D:/new2/dependencyJars.txt</outputFile>
            <overWriteIfNewer>true</overWriteIfNewer>
            <regenerateFile>false</regenerateFile>
        </configuration>
    </execution>
    <execution>
        <id>build-package2-dependencies</id>
        <phase>package</phase>
        <goals>
            <goal>build-classpath</goal>
            <goal>copy-dependencies</goal>
        </goals>
        <configuration>
            <appendOutput>true</appendOutput>
            <prefix>package2/</prefix>
            <outputAbsoluteArtifactFilename>true</outputAbsoluteArtifactFilename>
            <excludeGroupIds>com.test.package1</includeGroupIds>
            <outputDirectory>${project.build.directory}/package2-jars/</outputDirectory>
            <outputFile>D:/new2/dependencyJars.txt</outputFile>
            <overWriteIfNewer>true</overWriteIfNewer>
            <regenerateFile>false</regenerateFile>
        </configuration>
    </execution>    
</executions>

1 Answer 1

1

Is there any way to achieve the above thing, i.e, the first execution writes its dependencies to dependencyJars.txt, and then the second execution appends its dependencies in the file dependencyJars.txt?

That doesn't seem to be possible, no configuration is documented to prevent the file to be overriden. This behavior seems acceptable, otherwise when running the same build twice you'll have a duplication of the same classpath entries in the same file.

However you can configure each execution to write to a different file and then concat both files, for example using the Maven AntRun Plugin:

    <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
            <execution>
                <id>concat-build-classpath</id>
                <phase>package</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <concat destfile="D:/new2/dependencyJars.txt" >
                            <fileset file="D:/new2/dependencyJars1.txt" />
                            <fileset file="D:/new2/dependencyJars2.txt" />
                        </concat>
                    </target>
                </configuration>
            </execution>
        </executions>
    </plugin>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @Pierre B. That worked. But eventually I want the contents of text file 'dependencyJars.txt' to be written in the class-path field of the manifest file. Is this possible ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.