Executing post-build step Intellij IDEA CE 2020.3? - intellij-idea

I am using IntelliJ IDEA CE 2020.3 to build a simple JAR file. After the build, I'd like to copy the created JAR to a library directory.
I am using the Ant plugin that comes bundled with the IDE. I can't seem to find the underlying Ant build/control files that make the whole thing work. I assume Ant uses the .XML files that are part of the IDE's project settings, but this is unclear.
In any case, is there a way to add the "copy" step that I mention above?
Do I have to either use the built-in Ant or take it over completely myself?
Can I edit the default that ships with the IDE?

All,
So after some experimenting, I found that if I manually create the build.xml file, I can execute post-build steps. For my example here, I created the following, simple build.xml and added it to the top-level IntelliJ project directory (where the .iml file lives):
build.xml (manually created)
<project name="mylib" default="copy-file">
<target name="copy-file">
<copy file="out/artifacts/mylib_JAR/mylib.jar" tofile = "./mylib.jar" />
</target>
</project>
Note that the directories are relative to the project directory.
IntelliJ IDEA enabled me to add the copy-file task to augment the default, built-in build. To configure your tasks via the IDE, open the Ant tool window via the View/Tool Windows/Ant main menu item.
I hope this helps someone out there!

Related

Intellij IDEA no option to import ant build file

I have a project which uses ant build tool and the project uses
IntelliJ IDEA 2021.3.1 (Community Edition)
The most frustrating and infuriating part that I'm experiencing is
a process of importing build.xml file to ant window - it just doesn't
work whatever I try. And I've tried
Importing project from git
Importing project from sources
It just seems that the option to add build.xml file as an ant build file evades me.
The most ridiculous part is when I try to press + in an ant window and add build.xml it
does not allow me to select build.xml it just fails to show build.xml at all... Why?
What have I tried?
I've tried every single option written here
How to import existing Ant build.xml into IntelliJ IDEA
My questions
How to add buil.xml file to Intellij IDEA?
Why I'm failing to add build.xml file as an ant build file while
going though ant window + option?
Why I'm missing an option (right context menu) to add build.xml
as an ant build file?
I've tried to build project using Eclipse and everything was working fine, but Intellij
just ignores ant why? Do I need Ultimate version? Is ant option available only in paid version of Intellij IDEA?
Oh, my dear God, just found out how to import ant build.xml file
First I needed to override build.xml file type (click on an ant build.xml file -> select override file type -> select XML) and then second time I've clicked the right mouse button an extra option showed add as ant build file
showed up.

intellij - keymap / hotkey / shortcut per module / project

I have many projects
I use a hotkey to deploy files built externally to my deploy folder, as intellij is not doing that or is doing that very very unreliably and slowly.
so say in project A I have a shortcut CTRL+SHIFT+Z to deploy all files I need to the deploy folder with a shell script. In Project B I have a different shell script (obviously) to deploy all I need. But I dont want to create a new shortcut, I want to reuse CTRL+SHIFT+Z but that doesn't seem to work
Is there a way to define a shortcut / hotkey on module basis? or a different solution?
It is not possible at the moment, you can set several different keyboard layouts and switch them manually. Also there is a feature request to provide project level keymap: https://youtrack.jetbrains.com/issue/IDEA-127090
I just use always the similar ant build file in all projects doing a simple copy, and assign the shortcut to that ant build file:
so in each project I have a build.xml in the root with this content
I guess it works because the "target name" is always the same in each build.xml
<project name="joomlaPHC" default="copy-css" basedir=".">
<target name="copy-css"
description="copies css files" >
<copydir src="/home/myuser/IdeaProjects/MYPROJECT/src/public" dest="/var/www/html/MYPROJECT.local/public"/>
</target>
</project>
the shortcut seems to work
don't forget to assign the jdk
hope it helps

How do I import classes from one project's testpackages to another project's testpackages in netbeans?

In netbeans I can setup several classpaths for a project: compile, processor, run, compile tests, run tests.
In compile tests I added another project. I can now use that other project's src packaged classes, in my own project's test packages.
But I can't use that other project's test packaged classes.
How can I make this happen?
(Do I need to use maven projects instead of regular projects?)
I have found some solutions for this problem here. Suppose that you have a project B using the test classes of project A.
You can include {project A}/build/test/classes/ in the test compilation
classpath for project B.
An other option is to automatically create JAR file from the test classes of project A with this code in build.xml:
<target name="jar-tests" depends="compile-test">
<jar jarfile="testdist/tests.jar">
<fileset dir="${build.test.classes.dir}"/>
</jar>
</target>
Then you can add the JAR to the test compilation
classpath for project B.
The test compilation process can be extended with test JAR generation with this code inserted into build.xml:
<target name="-post-compile-test">
<jar jarfile="testdist/tests.jar">
<fileset dir="${build.test.classes.dir}"/>
</jar>
</target>
It updates the tests.jar every time you compile (or run) the tests.
In the Projects window you can expand the project that you are working on. Right Click on Libraries and select Add Project.... You will get a dialog allowing you to select another Netbeans Project with the Java classes that you wish to use.
OR
When you build the java project in netbeans it creates a jar file on the dist folder in the current project directory. You can use this jar file for the other project as a package by adding this as ADD jar in the next project.

logback.xml and running application from JetBrains IDEA IDE

When I develop application in IDEA, where should I place logback.xml for it to have an effect on the application?
It seems when you run/debug IDEA doesn't make any jars and doesn't invoke Maven to build something. Does it execute main() directly from compiled *.class file? If so, where can I put logback.xml so that it would have effect?
logback.xml should be available in the root directory of your CLASSPATH. When you run your application, the full CLASSPATH is printed at the very beginning. When I put logback.xml in /src/main/resources (Maven project) it works without any problem. Also putting it in /src/test/resources with logback-test.xml name has presence.
Simply run:
getClass().getClassLoader().getResource("/logback.xml");
And see whether it returns something or null.
If you are not working with Maven project, open Project structure (Ctrl + Alt + Shift + S) and add in Modules section select folder containing logback.xml and mark it as Sources (blue icon).
For me: I simply needed to re-build the project (not just re-run or re-compile the project). Changing the src\main\resources\logback.xml did not affect logback because the class loader is picking up the logback.xml that is in the target/ folder, not the /src folder. This makes sense since target is the run environment. Therefore, a build is required to transfer the new xml file over to target.

Using Maven ant task to install jar to local repository

At the end of my ant build id like it to call the equivalent of the command line call
mvn install:install-file -Dfile=my.jar -DgroupId=com.company.project -DartifactId=my_project -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
so that it will add the newly built jar to a maven repository which another project will rely on.
Ive tried using the maven-ant-task and have added the maven-ant-task jar to the ant built project and the following code to the build.xml:
<target name ="minstall" depends="jar">
<artifact:pom id="maven_install" file="maven_install.xml" />
<artifact:install file="${out.dir}/my_project.jar">
<pom refid="maven_install"/>
</artifact:install>
</target>
but seem to be missing something as it wont work for me. To begin with i get the error in the build.xml (ant build file) saying
The prefix "artifact" for element "artifact:pom" is not bound.
What am I doing wrong. I am fairly new to ant?
On a realted question what is the purpose of the associated POM file? I would not normally have a POM in this project as it is an ant build
Perhaps maven-ant-task jar is not installed, i.e. not in your ant CLASSPATH. You can follow this instruction for this.
As mentioned previously, you need to make sure the tasks are defined in your ant script, and the artifact namespace is understood.
The POM file is used (in this case) to tell the Maven repositories the dependencies of the JAR you are putting in the repository. The POM should also specify the JAR's identification information (groupId, artifactId, version number, license, etc.).
Strictly speaking, you do not need an external POM, you could define the information in your build.xml file as follows:
<!-- Assuming tasks defined, and 'artifact' namespace exists -->
<artifact:pom id="maven_install" groupId="com.whatever" artifactId="some-jar"
version="1.0" packaging="jar">
<dependency groupId="..." artifactId="..." version="..."/>
<dependency groupId="..." artifactId="..." version="..."/>
<license name="apache" url="http://www.apache.org"/> <!-- can be omitted -->
</artifact:pom>
<target name ="minstall" depends="jar">
<artifact:install file="${out.dir}/my_project.jar" pomRefId="maven_install"/>
</target>
When you install the JAR in the 'minstall' task, the POM should be generated with the appropriate dependencies in the local Repository.
That message means you are missing an xmlns:artifact attribute in your build.xml. Have a look at the installation page in the docs for an example.
As to the purpose of the POM file, it's mostly metadata so that maven can figure out dependencies properly. In a real maven build it also describes how to build, test and package. But in your case all that is done by ant instead.
I think that it makes no sense to put such commands in Ant's build.xml. If you want to have your jar file installed in your maven repo just use mvn install command.
Besides that, I guess that you are somehow confusing the purpose of Maven and Ant tools in your project. What I'd suggest is to use Maven as your main build tool. You can configure invokation of Ant targets in your POM file if you really need that. Personally, I think it is the best solution to have Ant called by Maven. Maven goals (such as clean, test, package, install and so on) are very simple to use and powerful (I guess that you can read it in every Maven tutorial).