java - Add a dependency from a jar file to be extracted from a zip which is coming from a maven repository - Stack Overflow

I want to download a zip file from a maven repository, extract the jar file from the zip, compile my sp

I want to download a zip file from a maven repository, extract the jar file from the zip, compile my springboot application with the jar file and include the jar into my application target jar file for execution purpose (doing a simple java -jar myApplication.jar)

  1. Is it possible to do all of those steps on the fly with a single mvn clean package command? If so what is the correct pom.xml?
  2. If not which manual steps must be performed with the correct pom.xml?

My Error on mvn clean package command :

[ERROR] Failed to execute goal on project service: 
Could not resolve dependencies for project com.team:service:jar:1.0.0-SNAPSHOT: Could not find artifact com.mycompany:my-library:jar:1.0 at specified path C:\my\service\target\unzipped\Bin\64bits\lib.jar -> [Help 1]

The error comes from dependency scope below as the target ${project.build.directory}/unzipped/Bin/64bits/lib.jar is not created yet when the dependency is invoqued. I know also that <scope>system</scope> is dedicated only for compilation purpose and I do not know how to have the jar for compile and for runtime i.e included into the target jar. my pom.xml :

    </dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>my-library</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.build.directory}/unzipped/Bin/64bits/lib.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com/local.work.service.BTKServiceApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-zip</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.group.HELLO</groupId>
                                <artifactId>HELLO-win-package</artifactId>
                                <version>3.5.1</version>
                                <type>zip</type>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}/unzipped</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.4.2</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Class-Path>${project.build.directory}/unzipped/Bin/64bits/lib.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    </build>

I want to download a zip file from a maven repository, extract the jar file from the zip, compile my springboot application with the jar file and include the jar into my application target jar file for execution purpose (doing a simple java -jar myApplication.jar)

  1. Is it possible to do all of those steps on the fly with a single mvn clean package command? If so what is the correct pom.xml?
  2. If not which manual steps must be performed with the correct pom.xml?

My Error on mvn clean package command :

[ERROR] Failed to execute goal on project service: 
Could not resolve dependencies for project com.team:service:jar:1.0.0-SNAPSHOT: Could not find artifact com.mycompany:my-library:jar:1.0 at specified path C:\my\service\target\unzipped\Bin\64bits\lib.jar -> [Help 1]

The error comes from dependency scope below as the target ${project.build.directory}/unzipped/Bin/64bits/lib.jar is not created yet when the dependency is invoqued. I know also that <scope>system</scope> is dedicated only for compilation purpose and I do not know how to have the jar for compile and for runtime i.e included into the target jar. my pom.xml :

    </dependencies>
        <dependency>
            <groupId>com.mycompany</groupId>
            <artifactId>my-library</artifactId>
            <version>1.0</version>
            <scope>system</scope>
            <systemPath>${project.build.directory}/unzipped/Bin/64bits/lib.jar</systemPath>
        </dependency>
    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com/local.work.service.BTKServiceApplication</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack-zip</id>
                    <phase>process-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.group.HELLO</groupId>
                                <artifactId>HELLO-win-package</artifactId>
                                <version>3.5.1</version>
                                <type>zip</type>
                                <overWrite>false</overWrite>
                                <outputDirectory>${project.build.directory}/unzipped</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.4.2</version>
            <configuration>
                <archive>
                    <manifestEntries>
                        <Class-Path>${project.build.directory}/unzipped/Bin/64bits/lib.jar</Class-Path>
                    </manifestEntries>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    </build>
Share Improve this question asked Mar 24 at 18:08 cknellecknelle 1673 silver badges14 bronze badges 2
  • 2 Why is a jar file your require for your build inside a ZIP file hosted in a maven repository? This sounds really weird. – Arno Commented Mar 24 at 18:31
  • The zip file contains the jni Jar file and its related dll. I can only unzip the zip file manually and use mvn install:install-file command in order to install the jar in my local repository – cknelle Commented Mar 27 at 13:18
Add a comment  | 

1 Answer 1

Reset to default 0

You can use the download-maven-plugin to download AND unzip the zip file in your target directory in a single step. Subsequently use the maven-resources-plugin to copy the jar to your target dir.

<build>
  ...
    <plugins>
      <plugin>
        <groupId>.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
      <plugin>
        <groupId>io.github.download-maven-plugin</groupId>
        <artifactId>download-maven-plugin</artifactId>
        <version>2.0.0</version>
        <executions>
          <execution>
            <id>download-zip</id>
            <phase>generate-resources</phase>
            <goals>
              <goal>wget</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <url>
            <!-- REPLACE BY YOUR URL TO YOUR DESIRED ZIP-->
             https://github/download-maven-plugin/download-maven-plugin/archive/refs/tags/2.0.0.zip</url>
          <unpack>true</unpack> <!-- unpacks the zip automatically-->
          <outputDirectory>${project.build.directory}/</outputDirectory>
        </configuration>
      </plugin>

      <plugin>
        <groupId>.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.3.1</version>
        <executions>
          <execution>
            <id>copy-jar</id>
            <phase>process-resources</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>${basedir}/target/destination-folder</outputDirectory>
              <resources>
                <resource>
                  <directory>
                    <!-- SPECIFY DIR USED IN ABOVE STEP -->
                    ${project.build.directory}/download-maven-plugin-2.0.0</directory>
                  <includes>
                    <include>
                        <!-- DEFINE JAR FILE -->
                        README.md
                    </include>
                  </includes>
                </resource>
              </resources>
            </configuration>
          </execution>
        </executions>
      </plugin>

  ...
</build>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744235044a4564432.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信