修订问题(以下老问题):
问题:使用Maven在NetBeans中构建和运行Jenkins插件项目时,log4j使用的 Java类加载器找不到某些文件(如配置文件,自定义appender,JDBC驱动程序).需要将它们复制到其他位置target,然后找到它们.
我认为我想要的解决方案是:我想给maven一个额外的命令行参数,在NetBeans中配置.这应该让maven在构建之后和运行之前做了额外的步骤.该步骤可以是文件复制(在pom.xml中指定,也可以由命令参数指定),也可以是运行脚本(在pom.xml中或命令行参数中指定).
要添加到pom.xml中的内容?一些maven插件?
旧问题(供参考)
我有一个关于"my-jenkins-plugin"的maven项目.我需要使用log4j.我目前在此路径上有log4j.xml:
./src/main/resources/log4j.xml
Run Code Online (Sandbox Code Playgroud)
当我清理和构建Netbeans时,它会被复制到这些地方:
./target/classes/log4j.xml
./target/generated-classes/emma/classes/log4j.xml
./target/my-jenkins-plugin/WEB-INF/classes/log4j.xml
Run Code Online (Sandbox Code Playgroud)
但是,log4j无法从任何这些位置找到该文件.我需要手动将其复制到此位置:
./target/work/webapp/WEB-INF/classes/log4j.xml
Run Code Online (Sandbox Code Playgroud)
然后它按预期工作.
更多细节:Maven 2,Netbeans 7.2.它是标准的Jenkins插件,最初创建的项目mvn hpi:create,Netbeans使用Jetty来运行Jenkins.在NetBeans下运行时使用的类路径java.class.path=C:\work\maven\boot\classworlds-1.1.jar(这是一个现有的jar)并且我没有找到向该类路径添加任何内容的方法,但是如果我将log4j.xml添加到该jar文件的根目录,它也可以找到并工作(显然这不是一个令人满意的解决方案).我知道log4j是否有效只是通过查看控制台输出,它是正确的日志行,还是可怕的log4j:WARN No appenders could be found for logger (TestLog). log4j:WARN Please initialize the log4j system properly.错误而没有别的.
问题:我怎么能有
And*_*wan 12
您可以使用Maven Ant插件来运行执行复制的Ant脚本,或使用Maven资源插件,这似乎针对您将内容复制到输出目录的确切用例.
另请注意,您可以按照此处所述参数化您的构建(忽略该标题的Jenkins部分,答案与Maven的一般用法有关).
小智 7
您可以在配置文件中使用此插件:
<pluginRepositories>
<pluginRepository>
<id>evgenyg</id>
<name>Evgeny Goldin repo</name>
<url>http://evgenyg.artifactoryonline.com/evgenyg/repo/com/github/goldin/</url>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>copy</id>
<build>
<plugins>
<plugin>
<groupId>com.github.goldin</groupId>
<artifactId>copy-maven-plugin</artifactId>
<version>0.2.5</version>
<executions>
<execution>
<id>create-archive</id>
<phase>generate-resources</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<resources>
<resource>
<targetPath>${project.build.outputDirectory}/work/webapp/WEB-INF/classes/</targetPath>
<directory>${project.basedir}/src/main/resources</directory>
<includes>
<include>log4j.xml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
激活: mvn -Pcopy generate-resources
这就是我如何让 Maven 在执行hpi:run目标之前将 .class 和 .jar 文件复制到新位置。log4j.xml在实例化 log4j 之前,仍然将其复制到我的 Java 代码中的一个static{}块中。为了使用此配置文件,我-P netbeans在 Netbeans 中添加了 Maven 命令行参数:
配置文件和插件定义在pom.xml:
<profiles>
<profile>
<id>netbeans</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<id>copy</id>
<phase>compile</phase>
<configuration>
<target>
<ant antfile="${basedir}/log4j/ant.xml">
<target name="log4jcopy"/>
</ant>
</target>
</configuration>
<goals>
<goal>run</goal> <!-- means antrun:run -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
Run Code Online (Sandbox Code Playgroud)
为 ant 构建 XML log4j/ant.xml,:
<project default="log4jcopy">
<target name="log4jcopy">
<echo message="Copying class files so that log4j finds them at runtime."/>
<copy verbose="true"
todir="target/work/webapp/WEB-INF/lib">
<fileset
dir="log4j"
includes="mysql-connector-java-5.1.6.jar" />
</copy>
<copy verbose="true"
todir="target/work/webapp/WEB-INF/classes/hyde/jenkins/myplugin">
<fileset
dir="target/classes/hyde/jenkins/plugins/myplugin"
includes="LogAppender*.class" />
</copy>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8854 次 |
| 最近记录: |