Maven shade-plugin没有将类复制到.jar文件

时间:2017-02-28 03:31:37

标签: scala maven executable-jar gatling maven-shade-plugin

我已下载Gatling Maven Example并尝试在其中添加mvn shade插件,如下所示,jar已创建,但它不包含任何类,因此在执行期间失败

E:\projects\gatling-maven>java -jar target\gatling-maven-plugin-demo-2.2.3.jar
Error: Could not find or load main class Engine

这是我添加的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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>io.gatling</groupId>
  <artifactId>gatling-maven-plugin-demo</artifactId>
  <version>2.2.3</version>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <gatling.version>${project.version}</gatling.version>
    <gatling-plugin.version>2.2.1</gatling-plugin.version>
    <scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>io.gatling.highcharts</groupId>
      <artifactId>gatling-charts-highcharts</artifactId>
      <version>${gatling.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>net.alchim31.maven</groupId>
        <artifactId>scala-maven-plugin</artifactId>
        <version>${scala-maven-plugin.version}</version>
      </plugin>
      <plugin>
        <groupId>io.gatling</groupId>
        <artifactId>gatling-maven-plugin</artifactId>
        <version>${gatling-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>execute</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

       <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <transformers>
                <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>Engine</mainClass>
                </transformer>
              </transformers>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

和包结构为

enter image description here

2 个答案:

答案 0 :(得分:2)

您必须将所有资源和scala文件移至class BST { private: struct Node { string value; Node * left; Node * right; }; Node * head; public: class LinkedList { private: struct LLNode { Node * val; LLNode * next; }; LLNode * head; LLNode * tail; public: LinkedList(); ~LinkedList(); void add(Node * s) { LLNode * p = new LLNode; p->val = s; p->next = NULL; if (head == NULL && tail == NULL) { head = p; tail = p; } else if (head == tail) { head->next = p; tail = p; } else { tail->next = p; tail = p; } } }; BST(); ~BST(); void test() { BST * test = new BST; LinkedList * newstack = new LinkedList; newstack->add(test); return; }; src\main\resources。 Shade插件不包含您的测试资源和scala文件。我也尝试了shadedTestjar,但它也不起作用。另一种选择可能是你使用

  1. Maven Dependency插件并手动移动所有依赖项 - 容易出错且难看
  2. 使用程序集插件 - 不适合
  3. 我尝试在src\main\scala中移动资源和scala文件,但它已经有效了。以下是工作内容,

    src/main

    希望它能解决你的问题。

    更改以下插件配置后尝试

    <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>io.gatling</groupId>
        <artifactId>gatling-maven-plugin-demo</artifactId>
        <version>2.2.3</version>
    
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <gatling.version>${project.version}</gatling.version>
            <gatling-plugin.version>2.2.1</gatling-plugin.version>
            <scala-maven-plugin.version>3.2.2</scala-maven-plugin.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>io.gatling.highcharts</groupId>
                <artifactId>gatling-charts-highcharts</artifactId>
                <version>${gatling.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>net.alchim31.maven</groupId>
                    <artifactId>scala-maven-plugin</artifactId>
                    <version>${scala-maven-plugin.version}</version>
                </plugin>
                <plugin>
                    <groupId>io.gatling</groupId>
                    <artifactId>gatling-maven-plugin</artifactId>
                    <version>${gatling-plugin.version}</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <configFolder>src/main/resources</configFolder>
                                <dataFolder>src/main/resources/data</dataFolder>
                                <resultsFolder>target/gatling/results</resultsFolder>
                                <requestBodiesFolder>src/main/resources/request-bodies</requestBodiesFolder>
                                <simulationsFolder>src/main/scala</simulationsFolder>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.3</version>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                            <configuration>
                                <transformers>
                                    <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                        <mainClass>Engine</mainClass>
                                    </transformer>
                                </transformers>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

        <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>scala-maven-plugin</artifactId>
                <version>${scala-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
    

    仍有文件未找到异常,但我相信它们很容易解决。

答案 1 :(得分:1)

首先了解Maven标准目录结构约定。您的主项目源现在放在src/test/下而不是正确的src/main/下,因此它们被视为用于单元测试。因此它不会打包。