6

I've followed the guide here to install a JAR file into my local repository.

I run following command:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=log4j-weblayout-0.0.1-SNAPSHOT.jar

The JAR file is built using Maven and contains a POM file inside it listing its dependencies. The file inside JAR has the path:

/META-INF/maven/in.ksharma/log4j-weblayout/pom.xml

Maven installs the artifact but does not read its POM. It creates an empty POM file which is void of any dependency info:

<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <modelVersion>4.0.0</modelVersion>
  <groupId>in.ksharma</groupId>
  <artifactId>log4j-weblayout</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <description>POM was created from install:install-file</description>
</project>

How can I ensure that the POM inside the JAR is the one that is installed?

Edit:

Contents of various files inside the JAR are as follows.

/META-INF/MANIFEST.MF:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: kshitiz
Created-By: Apache Maven 3.1.0
Build-Jdk: 1.8.0

/META-INF/maven/in.ksharma/log4j-weblayout/pom.properties:

#Generated by Maven
#Wed Oct 08 19:48:28 IST 2014
version=0.0.1-SNAPSHOT
groupId=in.ksharma
artifactId=log4j-weblayout

/META-INF/maven/in.ksharma/log4j-weblayout/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>in.ksharma</groupId>
    <artifactId>log4j-weblayout</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <url>https://github.com/Kshitiz-Sharma/log4j-weblayout</url>
    <properties>
        <maven.compiler.source>1.6</maven.compiler.source>
        <maven.compiler.target>1.6</maven.compiler.target>
    </properties>
    <dependencies>
        <!-- Compile dependencies -->
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity</artifactId>
            <version>1.7</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.jooq</groupId>
            <artifactId>joor</artifactId>
            <version>0.9.3</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <scope>compile</scope>
        </dependency>

        <!-- Provided dependencies -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
            <scope>provided</scope>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.0.5.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.1.8</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
</project>
OhadR
  • 8,276
  • 3
  • 47
  • 53
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • The question is: You have an existing pom which can be used to build the artifact...why not simply doing: `mvn install`? – khmarbaise Oct 08 '14 at 18:35
  • @khmarbaise What if I want to hand out the built JAR not code to another user? – Kshitiz Sharma Oct 09 '14 at 03:04
  • If you do an install it exactly installs only the jar file into the local repository. So what's the problem here? (to be clear a jar file can simply be extracted and class files can be decompiled)...I don't see the problem? – khmarbaise Oct 09 '14 at 15:45
  • Perhaps I'm not able to understand you. `mvn install` is used when you want to install directly from source code. I want to distribute a library file as a JAR for which `mvn install:install-file` is needed. – Kshitiz Sharma Oct 10 '14 at 05:10

3 Answers3

5

I think you'll need to extract the pom.xml from the jar and specify it using the pomFile property as follows:

mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file -Dfile=log4j-weblayout-0.0.1-SNAPSHOT.jar -DpomFile=pom.xml
Ryan Hoegg
  • 2,415
  • 2
  • 14
  • 15
  • 1
    I don't think that is necessary. The documentation states `If the JAR was built by Apache Maven, it'll contain a pom.xml in a subfolder of the META-INF directory, which will be read by default.` – Kshitiz Sharma Oct 09 '14 at 03:04
  • I don't see that in the documentation for the install-file goal: http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html . There are a few options that state that the groupId, artifactId, and version can be extracted from the included pom.xml, but I don't see the sentence you copied here. – Ryan Hoegg Oct 09 '14 at 15:42
  • 1
    That sentence is copied from the [link](http://maven.apache.org/plugins/maven-install-plugin/examples/custom-pom-installation.html) provided in the question. – Kshitiz Sharma Oct 18 '14 at 04:19
  • 3
    This is the correct answer. Even though Maven Install Plugin should be able to use the pom inside the JAR, the current version (2.5.2), as joanpau already mentioned, has a [bug](http://mail-archives.apache.org/mod_mbox/maven-users/201410.mbox/%3Cop.xnsamaeskdkhrr@robertscholte%3E) and doesn't do it. – John29 May 08 '15 at 20:35
2

Seems to be fixed in 3.0.0-M1 version of install plugin, but maven might still use 2.5.2 as a default. You can set the version of the install plugin in your pluginManagement section:

  <pluginManagement>
      <plugins>
      ...
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <version>3.0.0-M1</version>
        </plugin>
      ...
      </plugins>
  </pluginManagement>

or call the right version of the plugin on the command line:

mvn org.apache.maven.plugins:maven-install-plugin:3.0.0-M1:install-file -Dfile=log4j-weblayout-0.0.1-SNAPSHOT.jar
1

It seems to be an already fixed bug (in January 2015), although the fix is not in the latest release (version 2.5.2 released at 2014-08-27 according to plugins official site)

joanpau
  • 568
  • 5
  • 14
  • Theoretically, this bug was fixed and the fix release in 3.0.0, but I am still seeing the same issue a year later in 3.3.3. – Tao Starbow Nov 06 '17 at 20:32
  • I am seeing the same bug in 3.5.2. – Reenactor Rob Feb 19 '18 at 16:43
  • 1
    6+ years later and this is still an issue in 3.6.3. I'm guessing it's due to what @JensViebig said in his answer. Why is Maven still defaulting to using a version of `maven-install-plugin` with this bug?!? I see the plugin version has been updated in the documentation already linked to, but not on [this page](https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html). :'( – maltem-za Apr 29 '21 at 12:57
  • I also wish it was mentioned somewhere that one should check if the `pom.xml` installed in the local repository is correct. I've spent far longer than I'm willing to admit on determining that this was the root cause of a missing dependencies issue. Do people usually only install jars that have their respective dependencies rolled into them, or what am I missing? [Here](https://maven.apache.org/pom.html#Dependencies) is another page showing the `install-file` command with little other information. – maltem-za Apr 29 '21 at 13:28