Trying to copy a jar file at compile time (or packaging time) using maven and intellij

215 views Asked by At

I have a jar file project that I want to use in other projects. I want to copy the jar file to the other projects' folder. Ideally I would like the dependent project to compare the jar file in its folder and copy the library jar if it is newer. I am willing to accept having the library project copy the jar to the other projects every time it packages.

I have found many examples, but I am unable to get any of them to work for me.

Here is my pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>SqlPersistance</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-artifact</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>${project.groupId}</groupId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <version>${project.version}</version>
                                    <type>jar</type>
                                    <overWrite>true</overWrite>
                                    <outputDirectory>../TEST1</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The TEST1 folder is never ccreated.

2

There are 2 answers

4
Michael On

If you want to use one dependency in another maven project that is compiled locally, all you need to do is to run mvn clean install on the dependency project, this will add the jar to your local m2 repository. Then, you’ll need to add it as a dependency in another pom. When you will build the other maven project, maven will look for that dependency in your local m2 folder and will fetch it from there.

1
jordanthompson On

Thanks for everyone's help. It was a lot more obvious than I was thinking: In the jar project, I have it building the jar: file->Project Structure->artifacts (make sure that "Include in build project" is selected. You may also want to play with the output jar file name. Build the project. This should create your "out" folder in your project that contains the jar library. In the project you want to include the library, add this jar file to the its libraries: file->Project Structure->libraries (locate and add the newly built jar file.)

Now, whenever the jar library is updated, the project will have the latest and greatest.