简体   繁体   English

将依赖项从本地迁移到Maven

[英]Migrate dependencies from local to Maven

I have a Java code in which I added JARs using Maven and also from the local system. 我有一个Java代码,在其中我使用Maven以及从本地系统添加了JAR。 Now as I am running it on a different system, the local JARs are missing. 现在,当我在其他系统上运行它时,缺少本地JAR。 How can I add everything to Maven and remove the local class paths? 如何将所有内容添加到Maven并删除本地类路径?

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.memeanalytics</groupId>
<artifactId>kafka-consumer-storm</artifactId>
<packaging>jar</packaging>
<version>1.1.0</version>
<name>HibernateExample</name>
<url>http://maven.apache.org</url>


<repositories>

    <repository>
        <id>github-releases</id>
        <url>http://oss.sonatype.org/content/repositories/github-releases/</url>
    </repository>

    <repository>
        <id>clojars</id>
        <url>http://clojars.org/repo</url>
    </repository>

    <repository>
        <id>cloudera-repo-releases</id>
        <url>https://repository.cloudera.com/artifactory/repo/</url>
    </repository>

</repositories>

<dependencies>

    <!-- <dependency> <groupId>net.wurstmeister.storm</groupId> <artifactId>storm-kafka-0.8-plus</artifactId> 
        <version>0.4.0</version> </dependency> -->

    <dependency>
        <groupId>org.xerial.snappy</groupId>
        <artifactId>snappy-java</artifactId>
        <version>1.1.1.3</version> <!-- was 1.1.0-M3 -->
    </dependency>

    <dependency>
        <groupId>org.apache.storm</groupId>
        <artifactId>storm-kafka</artifactId>
        <version>0.9.3</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>1.6</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.sd.dwh.kafka.storm.Invoker</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

I don't know what you mean by "local JARs". 我不知道您所说的“本地JAR”是什么意思。 If you mean a mixture of Maven dependencies and manually added JARs into the IDE, then this is a very bad idea. 如果您要混合使用Maven依赖项并将其手动添加到IDE中,那么这是一个非常糟糕的主意。 Either you use Maven or not. 是否使用Maven。 If you use Maven, everything should be a Maven dependency. 如果使用Maven,则所有内容都应该是Maven依赖项。 In future, you need every manually added jars to be managed by Maven by adding it into the local repository: https://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html After you added it into the local repository, you can add it as dependency in your pom.xml as you do with all other dependencies from Maven central. 将来,您需要将每个手动添加的jar由Maven管理,方法是将其添加到本地存储库中: https : //maven.apache.org/guides/mini/guide-3rd-party-jars-local.html添加之后并将其添加到本地存储库中,您可以像从Maven Central来的所有其他依赖项一样,将其作为依赖项添加到pom.xml中。

If you share the project with other people or other computers, you run into the problem you were talking about. 如果您与其他人或其他计算机共享项目,则会遇到您所谈论的问题。 In a team (or inside a local network), you usually run a repository manager like Nexus . 在团队中(或在本地网络中),通常会运行Nexus之类的存储库管理器 When there are new dependencies which were manually added (and installed to the local repository) you usually deploy it to Nexus which acts as a proxy between your local network and Maven central. 当有新的依赖项被手动添加(并安装到本地存储库)时,通常将其部署到Nexus,该Nexus充当本地网络和Maven Central之间的代理。 Then, everybody in your team or local network can find these dependencies. 然后,您的团队或本地网络中的每个人都可以找到这些依赖项。

The easiest attempt to fix is to: 最简单的解决方法是:
1. Take note of all the jars/dependencies that you added manually to the classpath (using your IDE or otherwise) 1.记下您手动添加到类路径(使用IDE或其他方式)的所有jar /依赖项
2. Use maven central search facilities ( http://search.maven.org/ ) to locate those dependencies (one by one, of course) in public repositories. 2.使用Maven中央搜索工具( http://search.maven.org/ )在公共存储库中找到这些依赖项(当然是一个一个)。 The results you find using online maven searches will include a snippet of the pom for those dependencies (<dependency>...</dependency>) . 您使用在线Maven搜索找到的结果将包含这些依赖项的pom片段(<dependency>...</dependency>) Copy and paste them into your existing pom.xml's dependencies section. 将它们复制并粘贴到现有pom.xml的依赖项部分。 3. Remove your IDE-based classpath entries. 3.删除基于IDE的类路径条目。

Of course, this can only work if those dependencies are in public repositories . 当然,这仅在那些依赖项位于公共存储库中时才有效

You can setup a maven repository manager like nexus for your local development which can proxy the maven central repository and you can deploy your own library in it. 您可以为本地开发设置一个像nexus这样的maven存储库管理器,它可以代理maven中央存储库,并且可以在其中部署自己的库。 And when you need to develop on another environment, you can get the dependencies from your local maven repository. 而且,当您需要在其他环境上进行开发时,可以从本地Maven存储库获取依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM