I want to create a jar file from my project. This jar file not executable, just for library usage to be used in another java app. However, I do not know which maven plugin or dependency do this job. Please help..
I want only some specific packages in this jar file.. So I have to specify package names
You have to configure the included maven-jar-plugin as per the documentation.
<project>
...
<build>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<includes>
<include>**/foo/*</include>
</includes>
<excludes>
<exclude>**/bar/*</exclude>
</excludes>
</configuration>
</plugin>
...
</plugins>
</build>
...
</project>
You don't need a maven plugin. Just have
<packaging>jar</packaging>
and do mvn clean package. You should see the jar file in target/
Related
i'm currently confused that eclipse ignores my configured pom building instructions.
Used : Eclipse 2023-03 / 2022-12 (both same failure)
parent pom :
...
<modules>
<module>my-module</module>
<module>my-webapp</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>my-module</artifactId>
<version>1.0</version>
</dependency>
...
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<excludes>
<exclude>**/module.dtd</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>versions-maven-plugin</artifactId>
<version>2.14.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>17</source>
<target>17</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
my-module structure (pom refers to parent without any build instructions - packaging -> jar) :
src/main/java
---...
src/main/resources
src/main/resources/META-INF/test/file.xml
src/main/resources/example/bootstrap/file2.xml
my-webapp (refers to parent pom - packaging -> war) :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>${config.path}</directory>
<targetPath>WEB-INF/config</targetPath>
<includes>
<include>**/*</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
Build with mvn clean install results in a deployable war file which contains my-module.jar in lib folder as expected, size matches also the generated jar file in my-module targets folder.
so targets generated .war/.jar are correct.
Now i setup inside eclipse a default tomcat server above v.9.0.50+ (tried multiple versions, also latest 9.0.71). and added my-webapp(example-context) to the server (cleaned before and then publish).
at this point i inspected my unzipped war file inside tomcat webapproot (tried meta and tomcat location (both same failure)) , and was completly confused that my my-module-1.0.jar file only contains only META-INF/maven (pom.xml + pom.properties) and my java classes so that it doesen't match the size as expected (due to missing resource files).
Is there any trick or option to configure eclipse to build the artifact with resources as instructed by my poms or any other vaiable soloution ?
If i use intellij, it worked out of the box but i prefer eclipse for my project
Tried different versions of eclipse , tomcat , maven and plugin versions (latest) but nothing seems to work.
Sometimes if i change my resource or java files of my-mopdule, eclipse makes a redeploy and then the artifact contains the correct my-module-1.0.jar with resources included but after another mvn clean install its gone and i need to modify any files again up to 10 times if it happens again.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/Application.java</exclude>
</excludes>
</configuration>
</plugin>
this is my pom.xml , maven-compiler-plugin.version is 3.8.1 .
but i see the Applciation.class still in my jar package by maven
You are looking at the wrong location. From what I see in the screenshot, you've found some Application file from the External Libraries. What the maven-compiler-plugin does is to generate the target folder. That's where the class file should be excluded from. Check the existence of the file class under:
target/classes/...
And don't forget to run mvn clean install before (with emphasis on clean - this will wipe out your target folder)
In a project, I had to do a similar thing, due I need to exclude the module-info.java. I resolved using this configuration:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<configuration>
<excludes>
<exclude>**/module-info.java</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
If you want the entire project you can get it from GitHub. I hope this helps.
I am having problems getting Maven to build my webapp without including extraneous development file, such as unminified script and css files.
First i tried using exclude in combination with webResources
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<webResources>
<resource>
<directory>src/main/webapp/resources/</directory>
<excludes>
<exclude>annotated-js/*.js</exclude>
<exclude>compiled-js/*.js</exclude>
<exclude>css/*.css</exclude>
<exclude>less/*.less/exclude>
</excludes>
<directory>src/main/webapp/WEB-INF</directory>
<excludes>
<exclude>connection.json</exclude>
<exclude>reportRocket.jsp</exclude>
</excludes>
</resource>
</webResources>
</configuration>
</plugin>
</plugins>
</build>
The result was the contents of WEB-INF being duplicated in the project root and no excluded directories or files.
So, I looked around here and found this: maven2: excluding directory from WAR but running mvn clean package using either warSourceExcludes or packagingExcludes results in the directories i'm trying to exclude not being, well, excluded...
The build section of my pom.xml
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<warName>ReportRocket-1</warName>
<packagingExcludes>
src/main/webapp/resources/annotated-js/,
src/main/webapp/resources/compiled-js/,
src/main/webapp/resources/css/,
src/main/webapp/resources/less/,
src/main/webapp/WEB-INF/connection.json
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
Building the project with these settings results in the following project structure:
META-INF
resources
//desired folders
annotated-js
compiled-js
css
less
WEB-INF
// desired files
connection.json
This is my first time using a build tool, so i'm probably overlooking something simple but in the meantime, this is driving me crazy. Any suggestions or obvious problems with my xml?
First you should read the documentation of the maven-war-plugin cause the packagingExclude is intended for a different purpose, but in your case you need to do the configuration in that way:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>src/main/webapp/resources/</directory>
<!-- there's no default value for this -->
<excludes>
<exclude>annotated-js/**</exclude>
</excludes>
</resource>
</webResources>
</configuration>
In addition to the src/main/java, I am adding a src/bootstrap directory that I want to include in my build process, in other words, I want maven to compile and include the sources there in my build. How!?
You can use the Build Helper Plugin, e.g:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>add-source</id>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>some directory</source>
...
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
NOTE: This solution will just move the java source files to the target/classes directory and will not compile the sources.
Update the pom.xml as -
<project>
....
<build>
<resources>
<resource>
<directory>src/main/config</directory>
</resource>
</resources>
...
</build>
...
</project>
http://maven.apache.org/guides/mini/guide-using-one-source-directory.html
<build>
<sourceDirectory>../src/main/java</sourceDirectory>
also see
Maven compile with multiple src directories
With recent Maven versions (3) and recent version of the maven compiler plugin (3.7.0), I notice that adding a source folder with the build-helper-maven-plugin is not required if the folder that contains the source code to add in the build is located in the target folder or a subfolder of it.
It seems that the compiler maven plugin compiles any java source code located inside this folder whatever the directory that contains them.
For example having some (generated or no) source code in target/a, target/generated-source/foo will be compiled and added in the outputDirectory : target/classes.
To mark a folder as generated sources, AND have it picked up by IntelliJ, use the following:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<generatedSourcesDirectory>src/main/generated</generatedSourcesDirectory>
</configuration>
</plugin>
I spent an hour searching on how to avoid IntelliJ reverting after I manually marked target/generated-sources as a generated sources folder. The codehaus.mojo plugin didn't work. But this solution did!
You can add the directories for your build process like:
...
<resources>
<resource>
<directory>src/bootstrap</directory>
</resource>
</resources>
...
The src/main/java is the default path which is not needed to be mentioned in the pom.xml
How can I filter certain classes in /target/classes from going into /target/[webapp]/WEB-INF/classes? I want them compiled into /target/classes/ but not in the final war.
What are these classes for? If they are for testing, you can specify them in src/test/java, they will then be compiled into target/test-classes in the test-compile phase, but won't be included in the final war.
If they aren't for testing and aren't to be included in the war, perhaps they should be refactored into another project so you can specify it as a dependency (perhaps with "provided" scope so they won't be deployed.
For reference you can configure the war to include and exclude resources when packaging.
The following example will include all jpgs but exclude resources from the image2 sub folder:
<configuration>
<webResources>
<resource>
<!-- this is relative to the pom.xml directory -->
<directory>resource2</directory>
<!-- the list has a default value of ** -->
<includes>
<include>**/*.jpg</include>
<includes>
<excludes>
<exclude>**/image2</exclude>
</excludes>
</resource>
</webResources>
</configuration>
See the war plugin documentation for more details.
You can use the TrueZIP Maven Plugin ( http://mojo.codehaus.org/truezip-maven-plugin/ ).
See examples in:
http://svn.codehaus.org/mojo/trunk/mojo/truezip-maven-plugin/src/it/
You might have luck with this, assuming you them in a package that you can define with an ant pattern
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<excludes>**/dontneed/*.class</excludes>
</configuration>
</plugin>
With current version of maven-war-plugin (3.0.0) this works for me -
<profile>
<id>abc</id>
...
<build>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<packagingExcludes>WEB-INF/classes/com/abc/pqr/ClassName.class</packagingExcludes>
</configuration>
</plugin>
</build>
</profile>