maven install and deploy 3rd party dependencies with simple command line

后端 未结 3 643
长情又很酷
长情又很酷 2021-02-09 14:29

We have a number of third party dependencies that aren\'t hosted anywhere. For each of these we have a jar file that we\'d like to be able to install and/or deploy to our reposi

相关标签:
3条回答
  • 2021-02-09 14:46

    When Maven is missing a dependency, it will give you an error in it's output that contains the command line to issue to add the jar to a remote repository. That command line will automatically create a POM for you and upload the two together. Is that not sufficient for your needs?

    Using this facility, if I know that I have a dependency with no remote repository representation, I usually just go ahead and define it in my build with the GAV that I want, run the build once, then look at the build errors. Copy out the command for the deployment, then run that command. You'll want to make sure that you have the <repository> elements set up properly in the parent POM plus also set up corresponding <server> elements in your settings.xml with the repository upload password. Other than that, you should be good to go.

    I would add that you should check out Nexus before getting to far. It's worth the hassle to set up now! :-)

    0 讨论(0)
  • 2021-02-09 14:52

    Ok, I found a solution that allows me to run just mvn install or mvn deploy and have the jar file installed to the local or remote repository. Inspired by a post to the maven-users list and using the build-helper plugin, in the parent pom, I have:

    <pluginManagement>
        <plugins>
            <!-- Attach the jar file to the artifact -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.5</version>
                <executions>
                    <execution>
                        <id>attach-artifacts</id>
                        <phase>package</phase>
                        <goals>
                            <goal>attach-artifact</goal>
                        </goals>
                        <configuration>
                            <artifacts>
                                <artifact>
                                    <file>${artifactId}-${version}.jar</file>
                                    <type>jar</type>
                                </artifact>
                            </artifacts>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
    

    And then in the child poms, I have:

    <packaging>pom</packaging>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <version>...</version>
    ...
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    Some of the pieces of this that initially tripped me up:

    • The attach-artifact execution should be under <pluginManagement> so it doesn't get executed if you mvn install or mvn deploy the parent pom.
    • The children need to specify the build-helper-maven-plugin under the build plugins so that code from the parent <pluginManagement> gets run.
    • The children have to be declared as having <packaging>pom</packaging> because you can't add a jar to an artifact if it has the same name as the artifact.

    The only downside I see to this approach is that the artifact gets deployed as type pom instead of type jar. But I haven't seen any real consequences of that.

    0 讨论(0)
  • 2021-02-09 14:55

    I meet this problem in my work:

    Now I have a target.jar (it has a dependencies list : a.jar, b.jar, c.jar...), I want to use mvn install:install-file to put it into my local repo, but when I run the command blow

    mvn install:install-file -Dfile=/Users/username/.../target.jar -DgroupId=com.cnetwork.relevance  -DartifactId=target  -Dversion=1.0.0
    

    but when I use it I found there are many error, the jar which use target.jar cannot find a.jar, b.jar, c.jar, such as:

    com.cnetwork.a does not exist
    com.cnetwork.b does not exist
    com.cnetwork.c does not exist
    

    Then I went to ~/.m2/repository/.../target/1.0.0/target.pom to find the pom file of the target, but nothing in it!

    ...
    <groupId>com.cnetwork.relevance</groupId>
    <artifactId>target</artifactId>
    <version>1.0.0</version>
    ....
    # no dependencies about a/b/c.jar !!!
    

    This is what going wrong, the install:file -Dfile -DgroupId -D.. does not add dependencies into pom, I correct the answer by this method

    1. if you already have this maven project source, just install it into local repo

      mvn clean install

    2. if you have the jar and the pom of it, install jar with pom

      mvn install:install-file -Dfile=/.../.../target.jar -DpomFile=/.../../target.pom

    3. if you don't have a pom with target jar, write one and use upper command.

    4. if you cannot recover the pom file of it, may be you should give up.

    0 讨论(0)
提交回复
热议问题