【问题标题】:Maven not including jar files in classpath during packagingMaven 在打包期间不包括类路径中的 jar 文件
【发布时间】:2017-12-26 13:13:36
【问题描述】:

我刚开始使用 Maven,遇到了一个我无法解决的问题。我的应用程序运行所需的 jar 文件似乎不在类路径中。 Maven 不应该在 mvn 包期间处理这个问题吗?

当我运行 mvn package 时,我得到错误:

[ERROR] /home/dev/Desktop/maventest/my-app/src/main/java/com/mycompany/app/App.java:[79,9] cannot find symbol
  symbol:   class UpnpService
  location: class com.mycompany.app.App
[ERROR] /home/dev/Desktop/maventest/my-app/src/main/java/com/mycompany/app/App.java:[79,39] cannot find symbol
  symbol:   class UpnpServiceImpl
  location: class com.mycompany.app.App

示例代码确实说:“您需要类路径上的 cling-core.jar 及其依赖项(seamless-*.jar 文件)来构建和运行此代码。”

但这不是 maven 应该处理的事情吗?如果没有,我该如何包含这些文件?

这是我的 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>my-app</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-app</name>
  <url>http://maven.apache.org</url>

  <repositories>
    <repository>
      <id>4thline-repo</id>
      <url>http://4thline.org/m2</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.fourthline.cling</groupId>
      <artifactId>cling-core</artifactId>
      <version>2.1.1</version>
    </dependency>
  </dependencies>
</project>

这是我尝试运行的示例代码:

package com.mycompany.app;

import org.fourthline.cling.model.message.header.STAllHeader;
import org.fourthline.cling.model.meta.LocalDevice;
import org.fourthline.cling.model.meta.RemoteDevice;
import org.fourthline.cling.registry.Registry;
import org.fourthline.cling.registry.RegistryListener;

/**
 * Runs a simple UPnP discovery procedure.
 */
public class App {

    public static void main(String[] args) throws Exception {

        // UPnP discovery is asynchronous, we need a callback
        RegistryListener listener = new RegistryListener() {

            public void remoteDeviceDiscoveryStarted(Registry registry,
                                                     RemoteDevice device) {
                System.out.println(
                        "Discovery started: " + device.getDisplayString()
                );
            }

            public void remoteDeviceDiscoveryFailed(Registry registry,
                                                    RemoteDevice device,
                                                    Exception ex) {
                System.out.println(
                        "Discovery failed: " + device.getDisplayString() + " => " + ex
                );
            }

            public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
                System.out.println(
                        "Remote device available: " + device.getDisplayString()
                );
            }

            public void remoteDeviceUpdated(Registry registry, RemoteDevice device) {
                System.out.println(
                        "Remote device updated: " + device.getDisplayString()
                );
            }

            public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
                System.out.println(
                        "Remote device removed: " + device.getDisplayString()
                );
            }

            public void localDeviceAdded(Registry registry, LocalDevice device) {
                System.out.println(
                        "Local device added: " + device.getDisplayString()
                );
            }

            public void localDeviceRemoved(Registry registry, LocalDevice device) {
                System.out.println(
                        "Local device removed: " + device.getDisplayString()
                );
            }

            public void beforeShutdown(Registry registry) {
                System.out.println(
                        "Before shutdown, the registry has devices: "
                        + registry.getDevices().size()
                );
            }

            public void afterShutdown() {
                System.out.println("Shutdown of registry complete!");

            }
        };

        // This will create necessary network resources for UPnP right away
        System.out.println("Starting Cling...");
        UpnpService upnpService = new UpnpServiceImpl(listener);

        // Send a search message to all devices and services, they should respond soon
        upnpService.getControlPoint().search(new STAllHeader());

        // Let's wait 10 seconds for them to respond
        System.out.println("Waiting 10 seconds before shutting down...");
        Thread.sleep(10000);

        // Release all resources and advertise BYEBYE to other UPnP devices
        System.out.println("Stopping Cling...");
        upnpService.shutdown();
    }
}

示例代码来自:http://4thline.org/projects/cling/core/manual/cling-core-manual.xhtml#chapter.GettingStarted

非常感谢您的帮助。

【问题讨论】:

    标签: java maven


    【解决方案1】:

    看起来我使用的示例缺少导入语句:

    import org.fourthline.cling.UpnpService;
    import org.fourthline.cling.UpnpServiceImpl;
    

    【讨论】:

      【解决方案2】:

      Maven 会处理您在 pom.xml 中声明的依赖项,为了解决您的问题,您应该在 pom.xml 中添加 cling-core 依赖项

      <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.mycompany.app</groupId>
        <artifactId>my-app</artifactId>
        <packaging>jar</packaging>
        <version>1.0-SNAPSHOT</version>
        <name>my-app</name>
        <url>http://maven.apache.org</url>
      
        <repositories>
          <repository>
            <id>4thline-repo</id>
            <url>http://4thline.org/m2</url>
            <snapshots>
              <enabled>false</enabled>
            </snapshots>
          </repository>
        </repositories>
      
        <dependencies>
          <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
          </dependency>
          <dependency>
            <groupId>org.fourthline.cling</groupId>
            <artifactId>cling-core</artifactId>
            <version>2.1.1</version>
          </dependency>
          <!-- https://mvnrepository.com/artifact/org.fourthline.cling/cling-core -->
          <dependency>
            <groupId>org.fourthline.cling</groupId>
            <artifactId>cling-core</artifactId>
            <version>2.1.1</version>
          </dependency>
        </dependencies>
      </project>
      

      用上面的替换你的 pom.xml 并查看 maven 下载 jars,包括它的所有依赖项。

      【讨论】:

        猜你喜欢
        • 2012-02-15
        • 2023-03-18
        • 2013-01-31
        • 1970-01-01
        • 1970-01-01
        • 2010-09-16
        • 2012-04-05
        • 2023-03-11
        相关资源
        最近更新 更多