有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

找不到java Maven clean package主类

我有一个EclipseJava项目和以下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

我也尝试了shade插件,但没有任何运气

        <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>

只有在通过project在Eclipse中清理项目后,我才设法让它工作>;清洁>;清理选定的项目

这很奇怪。知道发生了什么吗


更新

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

mvn clean package
java -jar target/MyProject.jar

罐子的运行非常好

对这种行为有什么想法,或者可能有什么问题


更新2:发现错误

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

<sourceDirectory>src</sourceDirectory>

经验教训:如果不遵循Maven约定,请确保配置pom。正确使用xml。关闭线程


共 (0) 个答案