Maven:如何在建造后将我的jar安装到本地仓库

时间:2015-12-28 14:33:23

标签: java maven

我想构建一个包并将其安装到我的本地仓库。我的pom文件是:

<?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>com.me</groupId>
    <artifactId>MyApp</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <!-- This block declare a dependencies for this project -->
    <dependencies>

        <!-- Spring Boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

    </dependencies>

    <build>

        <!-- This block declare a plugins -->
        <plugins>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
            </plugin>

<!-- Installing to local repo -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <groupId>com.me</groupId>
                    <artifactId>MyApp</artifactId>
                    <version>1.0.0</version>
                    <packaging>jar</packaging>
                    <file>/Users/me/MyApp/target/MyApp-1.0.0.jar</file>
                    <generatePom>true</generatePom>
                </configuration>
                <executions>
                    <execution>
                        <id>install-jar-lib</id>
                        <goals>
                            <goal>install-file</goal>
                        </goals>
                        <phase>validate</phase>
                    </execution>
                </executions>
            </plugin>

        </plugins>
    </build>

</project>

但是,当我运行&#34; mvn package&#34;命令我得到一个没有文件的错误:/Users/me/MyApp/target/MyApp-1.0.0.jar所以我评论安装插件,运行&#34; mvn package&#34;,uncoment install plugin并运行& #34; mvn package&#34;。我可以一步到位吗?没有这个评论 - 取消评论的事情?

2 个答案:

答案 0 :(得分:5)

目前尚不清楚为什么需要调用install-file的{​​{1}}目标,但问题在于阶段。您使用maven-install-plugin配置时应该<phase>validate</phase>

看一下Introduction to the Build Lifecycle:阶段<phase>package</phase>是Maven执行的第一个阶段。这时,罐子没有建成,所以无法工作。

如果您确实希望在包阶段安装工件(运行validate时),则应将阶段设置为mvn package

请注意,您无需运行package,而只需拨打mvn package,而无需配置mvn install。使用此命令,工件将自动安装在本地仓库中。

答案 1 :(得分:0)

从我所知道的,mvn clean install,把包放在本地repo中,不需要任何插件