Maven安装无法从repo导入Selenium .jar



我正在尝试运行基本的Selenium示例。已经这样指定了pom.xml,以包含可执行.jar的入口点:

<?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>MySel20Proj</groupId>
    <artifactId>MySel20Proj</artifactId>
    <version>1.0</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>main.java.SeleniumTest</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.48.2</version>
        </dependency>
    </dependencies>
</project>

我可以运行Maven安装;但是,当我执行包含Selenium教程示例代码的jar时,我收到一个NoClassDefFoundError:/org/openqa/selenium/webdriver,这表明Maven安装Selenium -java jar失败了。

是否有一种方法来解决这个问题,而不添加硒jar到本地类路径?似乎没有从Maven Central构建的解决方案被认为是有害的。

我认为问题在于您没有将依赖的jar(在本例中是Selenium)添加到包含示例代码的jar中。您可以添加以下插件来将Selenium添加到示例代码jar中。

<build>
    <plugins>
      <!-- any other plugins -->
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>single</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
        </configuration>
      </plugin>
    </plugins>
  </build>

如何使用Maven创建具有依赖关系的可执行JAR ?

使用Maven在jar中包含依赖

最新更新