Intellij Idea build jar file without Infra.properties file Needed - java

Please Find the project Structure Image
I am trying to create the executable jar file for the sales order module as per the project structure. while building the jar, pointing to the sales order main class in execution package.
Also, I have added additional folders as a resource file. Infra.properties file in the project structure builds along with jar file. how to restrict that file, want use that file separately should not build in jar file itself

From your question it is not clear what build tool you are using.
If you are using IntelliJ IDEA specific build, then do next:
Open File > Settings...
Go to Build, Execution, Deployment > Compiler > Excludes
Add entry for the file you want to exclude, e.g.:
If you are using Maven specific build, then add exclude section to pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<excludes>
<exclude>**/infra.properties</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
Pattern here might be more specific of course.

Related

JavaFX Maven Plugin: com.zenjava - How to copy external resources

I'm relatively new to Maven + JavaFX and I'm trying to produce a JavaFX executable jar file with the com.zenjava maven plugin.
I was following this guide for reference:
https://www.youtube.com/watch?v=wbjW8rYlook
I have the following folder structure for my project:
Now I'm trying to run the config jfx:jar during maven build and was able to produce a jar file but the resources I need are not copied under the target/jfx/app folder.
Basically, I want to copy the entire src/main/resources folder to target/jfx/app/resources. How do I accomplish this?
Some information:
The src/main/resources/ folder will contain different kinds of files that I will need during runtime, (excel files, pdf, htmls...) and not just property files.
Thanks in advance.
========================================================================
UPDATE:
Yuri-M-Dias' answer helped.
Without changing any other setting, I managed to do this by just updating my pom file with:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>../jfx/app/resources</targetPath>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.7.0</version>
<configuration>
<mainClass>me.iamkenos.bayonetta.MainApp</mainClass>
</configuration>
</plugin>
</plugins>
</build>
This is definitely working but I'm not sure whether this is the best way, given I had to cheat it a bit by using "../" in <targetPath>../jfx/app/resources</targetPath> will wait for other possible answers for the meantime.
You can control Maven's output folders to specific folders using the resources keyword. For example, on my project:
<resources>
<resource>
<directory>src/main/java/view</directory>
<targetPath>view</targetPath>
</resource>
</resources>
I am forcing the contents of the java/view folder to output to the target/classes/view in this case, since it's where my JavaFX images and fxmls are. You can probably do the same for the jfx/app/resources folder.
As for copying the folder, you can take a look at the official maven recommendation.
When you run the command jfx:jar you will get executable jar file with resources folder inside because you added resources folder to the build path.
If you just copy the entire src/main/resources folder to target/jfx/app/resources folder you will have copies of the same resource files (inside and outside of generated jar file) and if you need to allow a user to edit some of resource files (e.g. *.properties files) your code I guess will rely on the inside files so user changes have no any effect in this case.
That is why you need to split project resources into:
Internal (the part of generated jar file e.g. raster graphics and read-only configs)
External (located outside the jar file e.g. config files that could be edited by user)
I would suggest to create 3 folders
\src\main\java (source code) - part of a build path
\src\main\resources (internal) - part of the build path
\src\main\config (external)- excluded from the build path
use maven to copy external config folder and build executable jfx jar
<build>
<resources>
<resource>
<directory>src/main/config</directory>
<targetPath>../jfx/app/config</targetPath>
</resource>
</resources>
</build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>8.8.3</version>
<configuration>
<vendor>${vendor}</vendor>
<mainClass>${mainClass}</mainClass>
<allPermissions>true</allPermissions>
</configuration>
</plugin>
</plugins>
The finishing touch is configuring symlink path to allow eclipse work in debug mode properly with external resources. You can use Link Shell Extension to do it.
for Windows it might look like
mklink /J C:\...\target\classes C:\...\target\jfx\app\config
LinkToFolder OriginalFolder
LinkToFolder is eclipse project folder with compiled classes

Maven changing output directory for jar doesn't work

I build my project with mvn clean install, however I want to generate a subfolder in the target folder and put the generated jar file in there. I saw these two questions Maven: specify the outputDirectory only for packaging a jar? and maven: how to place the output jar into another folder however their answer doesn't seem to work.
This is how my maven build looks like:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.3.1</version>
<configuration>
<outputDirectory>${project.build.directory}/blah</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
However I get the .jar file still in target directory. I also noticed that the project packaging is set as <packaging>eclipse-plugin</packaging> if I change this to jar, then it works fine, however I do need it to be eclipse-plugin. I am also using tycho for the eclipse plugin. Am I missing something that was not mentioned before?
From your packaging of eclipse-plugin I'm guessing you're using Tycho. Tycho doesn't seem to use any of the maven plugins, so configuring the maven-jar-plugin isn't going to help. Instead try configuring the tycho-packaging-plugin, specifically the buildDirectory property:
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-packaging-plugin</artifactId>
<version>${tycho-version}</version>
<configuration>
<buildDirectory>${project.build.directory}/blah</buildDirectory>
</configuration>
</plugin>

maven create jar with pom

I'm working on an Android project and have some core code(that has some dependencies) that i'd like to version and make into a library/artifact(?) that I can pull into other projects. I'm using Maven to build and my editor is IntelliJ. I've never created a .jar but I think that's what I want to do.
In IntelliJ, i've gone to File->Project Structure->Artifacts->+ but I'm lost. I don't know how to define which source directories and files to include in the jar and I'm unsure if I need to include the actual jars of its dependencies in the jar? or define those dependencies in a pom.xml file and include the pom.xml file in the jar?
Any clarification would be much appreciated.
Maven project's dependencies are defined in its pom.xml file (basically, changing the dependencies from here is what are you visually doing using Intellij, by File->Project Structure->Artifacts). The source files that Maven takes are the ones which are into the src/main directory of your project. The build is done by default excluding the dependencies, so if you want to include them in your final jar, you have to specify this in your pom file (plugins):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
After that you have to execute mvn install command to have your jar created. You can do it running maven externally or from your IDE (if you have some Maven plugin installed) and the jar will be created in project's target folder.
The settings in IntelliJ IDEA will only create the JAR when you build using the IntelliJ IDEA compiler. To have maven create the JAR and deploy it to the maven repository, you need to use the maven assembly plugin. There is a predefined configuration for creating a jar with dependencies.
The Maven Shade Plugin can also create JARs and handle more advanced use cases. But you'll probably want to start with the assembly plugin/

Is it possible to include a dependency at a specific phase of the build?

I am currently using the maven-jar-plugin to build a JAR from a specific class folder which I have included as a provided scope dependency. I now need to include this JAR as a dependency in my final build.
So, is it possible to include a dependency at a specific phase of the build?
NOTE: The dependency is built during the build process, so it is only available during the compile phase.
The simplest way to tackle this would be to separate the code you are building with the maven-jar-plugin into a separate Maven project, and just refer to it as a normal dependency in the main Maven project.
This way you don't need to mess around with having one Maven build produce two different artifacts (the JAR you are generating and then the main output minus that JAR's classes) while having the output of the first being referred to in the second.
Alternatively, assuming your main project has the package type set to jar, you could just add the "specific class folder" classes to the project's <resources> element and then configure/override the maven-jar-plugin execution that runs during the package phase to exclude these classes, with the <excludes> element in the <configuration>.
You could configure the files included in the generated jar :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<includes>
<include>relative_path_to_your_jar</include>
<include>**/**</include>
</includes>
</configuration>
</plugin>
http://maven.apache.org/plugins/maven-jar-plugin/jar-mojo.html#includes

How do I add an extra source directory that will be used by the maven-jxr-plugin?

I'm using the build-helper-maven-plugin to add it to my build, but I'd
like to see the XREF source for this extra source directory as well.
FYI:
maven-jxr-plugin - The JXR plugin produces a cross-reference of the project's sources. The generated reports make it easier for the user to reference or find specific lines of code. It is also handy when used with the PMD plugin for referencing errors found in the code.
build-helper-maven-plugin - This plugin contains various small independent goals to assist with Maven build lifecycle.
You can tell JXR what files to index using a file pattern, following Ant guidelines. For example, to include all java files in src/main/java and all source in src/main/java2, the following configuration in your file should work:
<project>
...
<reporting>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jxr-plugin</artifactId>
<configuration>
...
<includes>
<include>src/main/java/**/*.java</include>
<include>src/main/java2/**/*.java</include>
<includes>
...
</configuration>
</plugin>
</plugins>
...
</reporting>
...
</project>
The problem I was having was related to the order of the plugins in the pom.xml.
When I moved the jxr plugin to the top of the plugin list everything started working.
Crazy stuff.

Categories

Resources