java 如何将源上传到本地 Maven 存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4031987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:28:05  来源:igfitidea点击:

How to upload sources to local Maven repository

javamaven-2maven

提问by abovesun

Suppose I have a Maven 2 Java project on my local machine. I build the project .jarfile and push it to my local Maven repo, using mvn install.

假设我的本地机器上有一个 Maven 2 Java 项目。我构建项目.jar文件并将其推送到我的本地 Maven 存储库,使用mvn install.

Question:

问题:

How can I force Maven to also push the project sources jarto the local repo?

如何强制 Maven 也将项目源推jar送到本地存储库?

This is useful if I'll use the above mentioned project as dependency while developing a new project, and can use the mvn eclipse:eclipse -DdownloadSourcesfeature.

如果我在开发新项目时将上述项目用作依赖项,并且可以使用该mvn eclipse:eclipse -DdownloadSources功能,这将非常有用。

回答by Sean Patrick Floyd

This snippet automatically installs / deploys a source jar from any install / deploy:

此代码段从任何安装/部署中自动安装/部署源 jar:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <version>[whatever version is current]</version>
        <executions>
          <execution>
            <id>attach-sources</id>
            <phase>verify</phase>
            <goals>
              <goal>jar-no-fork</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  ...
</project>

Use this link to check for the current version of the maven-source-plugin

使用此链接检查当前版本的maven-source-plugin

Or from command line:

或者从命令行:

mvn clean source:jar install

回答by Vokail

I have found a better answer, just add this on your pom.xml

我找到了更好的答案,只需将其添加到您的 pom.xml 中

 <build>
      <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-source-plugin</artifactId>
        <executions>
            <execution>
                <id>attach-sources</id>
                <goals>
                    <goal>jar</goal>
                </goals>
            </execution>
        </executions>
       </plugin>
     </plugins>
  </build>

And run from command line:

并从命令行运行:

mvn install

Now maven install on your local repository jar and sources

现在 maven 安装在您的本地存储库 jar 和源上

Solution found on: https://www.mkyong.com/maven/generate-source-code-jar-for-maven-based-project/(I'm not affiliated in any way)

解决方案在:https: //www.mkyong.com/maven/generate-source-code-jar-for-maven-based-project/(我不以任何方式附属)

回答by Lonzak

One addition to the above answer:

对上述答案的补充:

It still wasn't working correctly for me. The sources.jar was generated and available in the target folder but not in the local repository. The reason for that was that I specified the phase differently: <phase>install</phase>In this case the maven install plugin is executed first and copies the jar files into the local repo. Afterwards the source.jar is generated (and thus not copied).

它对我来说仍然不能正常工作。source.jar 已生成并在目标文件夹中可用,但不在本地存储库中。这样做的原因是我指定了不同的阶段:<phase>install</phase>在这种情况下,首先执行 maven install 插件并将 jar 文件复制到本地存储库中。然后生成 source.jar(因此不会被复制)。

[INFO] --- maven-install-plugin:2.4:install (default-install) @ foo ---
[INFO] Installing foo.jar into local repo
[INFO] Installing pom.xml into local repo
[INFO] Installing javadoc.jar into local repo
[INFO]
[INFO] --- maven-source-plugin:3.0.1:jar-no-fork (attach-sources) @ foo ---
[INFO] Building jar: foo-sources.jar

So it is important to specify an earlier phase than install (like it is correctly mentioned in the accepted answer).

因此,指定比安装更早的阶段很重要(就像在接受的答案中正确提到的那样)。