Maven 打包生成jar,执行报错,ClassNotFoundException:org/apache/loggin/log4j/LogManager

简单得程序代码:
public class App {
    private static Logger logger = LogManager.getLogger(App.class);
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 100; i++) {
            Thread.sleep(1000);
            logger.error("开始执行:" +  i);
        }
    }
}

执行报错;


出现上述得原因是:main打包的时候,并不会自动得将pom.xml里面引入的包一起打到jar里面,需要在pom里面配置插件代码:
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.4.1</version>
            <configuration>
                <archive>
                    <manifest>
                        <!--这里要替换成jar包main方法所在类 -->
                        <mainClass>com.App</mainClass>
                    </manifest>
                    <manifestEntries>
                        <Class-Path>.</Class-Path>
                    </manifestEntries>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

猜你喜欢

转载自blog.csdn.net/waterflying2015/article/details/81189037