Maven清洁包主类未找到

时间:2017-03-14 15:58:33

标签: java eclipse maven

我有一个Eclipse Java项目和以下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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

    <!-- Initial stuff not include for simplicity -->

    <!-- Repositories -->
    <repositories>
    </repositories>  

    <!-- Dependencies -->
    <dependencies>
    </dependencies>


    <!-- Build  -->  
    <build>
        <finalName>MyProject</finalName>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>false</downloadJavadocs>
                </configuration>
            </plugin>

            <!-- Set a compiler level -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>

            <!-- Maven Assembly Plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                      <manifest>
                        <mainClass>main.ParserEngine</mainClass>
                      </manifest>
                    </archive>
                </configuration>
                <executions>
                  <execution>
                    <id>make-assembly</id>
                    <!-- bind to the packaging phase -->
                    <phase>package</phase> 
                    <goals>
                        <goal>single</goal>
                    </goals>
                  </execution>
                </executions>
            </plugin>


            <!-- Resources managing plugin -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <executions>
                        <execution>
                            <id>copy-resources</id>
                            <!-- here the phase you need -->
                            <phase>package</phase>
                            <goals>
                              <goal>copy-resources</goal>
                            </goals>
                            <configuration>
                              <outputDirectory>${basedir}/target/</outputDirectory>
                              <resources>          
                                <resource>
                                  <directory>models/</directory>
                                  <filtering>false</filtering>
                                </resource>
                              </resources>              
                            </configuration>
                        </execution>
                    </executions>
              </plugin>


            <!-- Clean plugin  -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.6.1</version>
                <configuration>
                      <filesets>
                            <fileset>
                                  <directory>${basedir}</directory>
                                  <includes>
                                    <include>output.txt</include>
                                  </includes>
                              <followSymlinks>false</followSymlinks>
                            </fileset>
                      </filesets>
                </configuration>
          </plugin>

        </plugins>
  </build>  

当我跑步时

mvn clean package

我得到了

Error: Could not find or load main class main.ParserEngine

我也尝试了阴影插件,但没有任何运气。

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

我设法让它在Eclipse中通过Project&gt;清理项目后才能工作。清洁&gt;清理选定的项目。

这很奇怪。有什么想法发生了什么?

更新

我创建了一个简单的Eclipse项目(包括一些库依赖项)并使用了相同的Maven pom.xml。然后我跑了:

mvn clean package
java -jar target/MyProject.jar

并且jar执行完全正常。

有关此行为或可能出现的错误的任何想法?

更新2:发现错误

显然,Eclipse项目没有遵循Maven conventions,并且pom.xml没有对这些约定进行必要的更改,例如,缺少以下内容

<sourceDirectory>src</sourceDirectory>

获得的经验:如果您不遵循Maven约定,请确保正确配置您的pom.xml。关闭线程。

1 个答案:

答案 0 :(得分:0)

<sourceDirectory>src</sourceDirectory>

这并不能解决我的问题。每次我运行maven clean时,都找不到我的主班。在我运行maven install或maven clean之后,它又可以工作了。...