如何在运行java -cp时使用maven的repo作为我的类路径

时间:2018-01-08 03:01:50

标签: java maven

我在dir / opt / myApp

中运行java应用程序

我在/ opt / myApp / lib

中都需要罐子

我使用的cmd是java -cp /opt/myApp/lib/* org.my.App

但每次我重新编译我的代码(maven项目)时,我都需要将所有lib文件从工作区复制到/ opt / myApp / lib。

我想知道是否可以将maven的repo用作我的类路径以避免手动复制所有jar?

到目前为止我的进展:

我在/ opt / myApp / config中也有一些配置文件,所以我不能直接在maven项目下运行它。

也许我可以将/ opt / myApp作为maven项目并在pom.xml中执行某些操作,以便我可以通过maven运行应用程序?

1 个答案:

答案 0 :(得分:0)

我自己想出了一个解决方案:

在/ opt / myApp中创建一个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>my</groupId>
    <artifactId>my-App-env</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <properties>
            <org.springframework-version>4.3.5.RELEASE</org.springframework-version>
    </properties>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>
    <!-- repositories> <repository> <id>twitter-twttr</id> <url>http://maven.twttr.com/</url> 
            </repository> </repositories -->
    <dependencies>
            <dependency>
                    <groupId>my</groupId>
                    <artifactId>my-app</artifactId>
                    <version>1.0.0</version>
                    <type>test-jar</type>
            </dependency>
    </dependencies>
    <build>
              <resources>
                      <resource>
                              <directory>config</directory>
                      </resource>
                      <resource>
                              <directory>src/main/resources</directory>
                      </resource>
              </resources>
              <plugins>
                      <plugin>
                              <groupId>org.codehaus.mojo</groupId>
                              <artifactId>exec-maven-plugin</artifactId>
                              <version>1.6.0</version>
                              <executions>
                                      <execution>
                                              <id>test</id>
                                              <goals>
                                                      <goal>java</goal>
                                              </goals>
                                      </execution>
                              </executions>
                      <configuration>
                              <mainClass>org.my.App</mainClass>
                      </configuration>
                    </plugin>
            </plugins>
    </build>

然后我可以通过

运行应用程序
mvn exec:java

并且exec-maven-plugin会自动生成类路径。