Maven构建未输出.class文件?

huangapple go评论93阅读模式
英文:

Maven package not outputting .class file?

问题

我正在使用Maven 3.6.3编译、构建并将我的项目打包成jar,在运行mvn package命令后,jar内部没有任何*.class文件,只有在编译时生成的.java*文件,这不是正确的输出。正确的方法是什么?

以下是我的文件夹结构

ProjectA
-- src
      com
        hcc
          Folder1
            a.java
            b.java
-- META-INF
      services
        service1.txt
        service2.txt
-- pom.xml

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/maven-v4_0_0.xsd">
    
    <modelVersion>4.0.0</modelVersion>
    <groupId>Utility</groupId>
    <artifactId>UtilityDev</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0-SNAPSHOT</version>
    
    <properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <base.path>${project.basedir}/../ThirdParty Jars</base.path>
	</properties>

    <build>
        <finalName>Utility</finalName>
        <resources>
            <resource>
                <directory>src</directory>
            </resource>
            <resource>
                <directory>META-INF</directory>
                <includes>
                    <include>**services/**</include>
                </includes>
                <targetPath>META-INF</targetPath>
            </resource>
        </resources>
        <plugins>
            <plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-jar-plugin</artifactId>
                <version>3.2.0</version>
				<configuration>
		            <excludes>
                        <exclude>*.properties</exclude>
		            </excludes>
	                <archive>
	                    <addMavenDescriptor>false</addMavenDescriptor>
                        <manifest>
                            <addDefaultImplementationEntries>false</addDefaultImplementationEntries>
                        </manifest>
	                </archive>
            	</configuration>
			</plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>xyz</groupId>
            <artifactId>xyz</artifactId>
            <version>123</version>
            <scope>system</scope>
            <systemPath>${base.path}/CommonLib/xyz.jar</systemPath>
        </dependency>
    </dependencies>
</project>
英文:

I am using Maven 3.6.3 to compile, build and package my project into jar, after running mvn package command so there are no .class files inside the jar all are .java files which were generated at the time of compilation so which is not the correct output, what is the correct way to do it?

Below is my folder structure

ProjectA
-- src
      com
        hcc
          Folder1
            a.java
            b.java
-- META-INF
      services
        service1.txt
        service2.txt
-- pom.xml

pom.xml

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;&gt;
    
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
    &lt;groupId&gt;Utility&lt;/groupId&gt;
    &lt;artifactId&gt;UtilityDev&lt;/artifactId&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;version&gt;1.0.0-SNAPSHOT&lt;/version&gt;
    
    &lt;properties&gt;
		&lt;maven.compiler.source&gt;1.8&lt;/maven.compiler.source&gt;
		&lt;maven.compiler.target&gt;1.8&lt;/maven.compiler.target&gt;
        &lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
        &lt;base.path&gt;${project.basedir}/../ThirdParty Jars&lt;/base.path&gt;
	&lt;/properties&gt;

    &lt;build&gt;
        &lt;finalName&gt;Utility&lt;/finalName&gt;
        &lt;resources&gt;
            &lt;resource&gt;
                &lt;directory&gt;src&lt;/directory&gt;
            &lt;/resource&gt;
            &lt;resource&gt;
                &lt;directory&gt;META-INF&lt;/directory&gt;
                &lt;includes&gt;
                    &lt;include&gt;**services/**&lt;/include&gt;
                &lt;/includes&gt;
                &lt;targetPath&gt;META-INF&lt;/targetPath&gt;
            &lt;/resource&gt;
        &lt;/resources&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
				&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
				&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
                &lt;version&gt;3.2.0&lt;/version&gt;
				&lt;configuration&gt;
		            &lt;excludes&gt;
                        &lt;exclude&gt;*.properties&lt;/exclude&gt;
		            &lt;/excludes&gt;
	                &lt;archive&gt;
	                    &lt;addMavenDescriptor&gt;false&lt;/addMavenDescriptor&gt;
                        &lt;manifest&gt;
                            &lt;addDefaultImplementationEntries&gt;false&lt;/addDefaultImplementationEntries&gt;
                        &lt;/manifest&gt;
	                &lt;/archive&gt;
            	&lt;/configuration&gt;
			&lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

    &lt;dependencies&gt;
        &lt;dependency&gt;
            &lt;groupId&gt;xyz&lt;/groupId&gt;
            &lt;artifactId&gt;xyz&lt;/artifactId&gt;
            &lt;version&gt;123&lt;/version&gt;
            &lt;scope&gt;system&lt;/scope&gt;
            &lt;systemPath&gt;${base.path}/CommonLib/xyz.jar&lt;/systemPath&gt;
        &lt;/dependency&gt;
    &lt;/dependencies&gt;
&lt;/project&gt;

答案1

得分: 1

不要将src添加到资源中。

相反,将您的源代码放入src/main/java,Maven会自动找到它。

英文:

Don't add src to the resources.

Instead, put your source code into src/main/java and Maven will find it automatically.

huangapple
  • 本文由 发表于 2020年4月7日 21:57:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/61081707.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定