我们自己的项目肯定会定义一份log4j的配置文件,如果我们import的jar包里也包含配置文件,是否会有冲突?
因为import的jar包最终也会加入到classpath,所以执行时,import的配置文件和我们直接定义的配置文件都会出现在classpath中,最终到底用哪一个取决于先加载到哪一个配置文件,与classpath中含有相同全类名的情况是一样的,具有不确定性。所以,一般建议不要在jar包里加入配置文件,可能会影响覆盖项目本身的配置文件,一旦发现jar包有额外的配置文件,可以选择exclude掉。
Note
Please, DO NOT include the log4j.properties
into the final Jar file, it will cause multiple log4j.properties
files in the classpath, if someone is depending on your Jar, you may accidentally override their logging configurations, depends which Jar is loaded first.