如何正确构建java maven项目的jar文件?

时间:2017-02-07 12:56:28

标签: java maven

这是我的POM文件

<?xml version="1.0" encoding="UTF-8"?>
<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>
<groupId>com.tacton.tcsite</groupId>
<artifactId>asdf</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<description>Plugin provides testing for my first application</description>
<name>Hello World Application</name>



<properties>
    <outputDir>${project.build.directory}/output</outputDir>
    <profileName>tcsite-deploy</profileName>
    <tcsite.deploy.dir>C:/Program Files/TCsite standard    3.4.1/tomcat/webapps/ROOT/WEB-INF/plugins</tcsite.deploy.dir>

    <!-- Add custom build final name-->
    <plugin.finalName>${project.artifactId}.${project.version}         </plugin.finalName>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

<build>
    <finalName>${plugin.finalName}</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>tcsite-repository-releases</id>
        <name>tcsite-repository-releases</name>
        <url>http://artifactory.tacton.com/artifactory/tcsite</url>
        <releases>
            <enabled>true</enabled>
            <checksumPolicy>fail</checksumPolicy>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<profiles>
    <profile>
        <id>tcsite-deploy</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>tcsite-deploy-dir</name>
                <value>${tcsite.deploy.dir}</value>
            </property>
        </activation>
        <build>
            <finalName>${plugin.finalName}</finalName>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <dependencies>
                        <dependency>
                            <groupId>ant-contrib</groupId>
                            <artifactId>ant-contrib</artifactId>
                            <version>20020829</version>
                            <exclusions>
                                <exclusion>
                                    <groupId>ant</groupId>
                                    <artifactId>ant</artifactId>
                                </exclusion>
                            </exclusions>
                        </dependency>
                        <dependency>
                            <groupId>org.apache.ant</groupId>
                            <artifactId>ant-nodeps</artifactId>
                            <version>1.8.1</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <configuration>
                                <tasks>
                                    <taskdef resource="net/sf/antcontrib/antcontrib.properties"
                                             classpathref="maven.plugin.classpath"/>
                                    <if>
                                        <!-- Remember to set -DSKIP_DEPLOY=true in Jenkins Build Job configuration in order
                                         to ignore copying the file to ${tcsite.deploy.dir} -->
                                        <!-- TISD-1405 -->
                                        <isfalse value="${SKIP_DEPLOY}"/>
                                        <then>
                                            <copy
                                                    file="${project.build.directory}/${project.build.finalName}.${project.packaging}"
                                                    todir="${tcsite.deploy.dir}" verbose="true"
                                                    failonerror="false"/>
                                        </then>
                                    </if>
                                </tasks>
                            </configuration>
                            <goals>
                                <goal>run</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.gmavenplus</groupId>
                    <artifactId>gmavenplus-plugin</artifactId>
                    <version>1.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>testCompile</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <testSources>
                            <source>
                                <directory>${project.basedir}/src/test/groovy</directory>
                                <includes>
                                    <include>**/*.groovy</include>
                                </includes>
                            </source>
                        </testSources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

这是我的目录结构

enter image description here

问题是当我用maven构建它时。它创建了jar文件但该jar文件不包含plugin目录中的文件。 helloWorld.js和plugin.xml。这个问题将如何解决?

1 个答案:

答案 0 :(得分:2)

Maven不会将src/main/plugin文件夹识别为源文件夹或资源文件夹 只需将插件文件夹移动到src/main/resources即Maven中的默认资源目录,它就可以解决您的问题。