flyway.properties file not working as pom.xml configuration alternative - java

I need to set flyway migration and I don't want to put password and username in pom.xml, I created flyway.properties file, but it's not working, I'm getting this error
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.1:clean (default-cli) on project entesting: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
flyway.properties file is in same directory as pom.xml
flyway.user=sa
flyway.password=passwordForThis
flyway.url=jdbc:sqlserver://172.23.176.144;database=DB_Name
flyway.locations=filesystem:src/main/resources/db/migration
flyway.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
pom.xml flyway plugin config:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.1</version>
<configuration>
<url>jdbc:sqlserver://172.23.176.144;database=DB_Name</url>
<user>sa</user>
<password>passwordForThis</password>
</configuration>
</plugin>
To summarize I don't want to add password, username etc. in pom.xml(that works), I want it to be in flyway.propeties.

Flyway can be configured in a number of ways beyond directly putting it into the maven pom.xml. See documentation on configuring the maven plugin
The contents of the properties file you showed looks like the contents of a flyway.conf file.
Flyway will search for and automatically load the <user-home>/flyway.conf config file if present.
It is also possible to point Flyway at one or more additional config files. This is achieved by supplying the System property flyway.configFiles as follows:
mvn -Dflyway.configFiles=path/to/myAlternativeConfig.conf flyway:migrate
See https://flywaydb.org/documentation/maven/#config-files for more information.
Alternatively for storing the database user and password, Maven settings.xml files can also be used:
<settings>
<servers>
<server>
<!-- By default Flyway will look for the server with the id 'flyway-db' -->
<!-- This can be customized by configuring the 'serverId' property -->
<id>flyway-db</id>
<username>myUser</username>
<password>mySecretPwd</password>
</server>
</servers>
</settings>

Related

Azure Artifacts deploy packages

I am trying to publish third-party private package directly to my Azure Artifacts feed with mvn deploy:deploy-file command like this:
mvn deploy:deploy-file -Dpackaging="jar" -DrepositoryId="PXXXXX-inccoming" -Durl="https://pkgs.dev.azure.com/XxxXxxxXxxx/XxxXxx/_packaging/PXXXXX-incoming/maven/v1" -DgroupId="pl.group.id" -DartifactId="artifact" -Dversion="0.0.2" -Dfile="C:\path\pl\group\id\0.0.2\artifact-0.0.2.jar"
But I am getting strange error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default-cli) on project CAST_2015: Failed to deploy artifacts: Could not transfer artifact pl.group.id:artifact:jar:0.0.2 from/to PXXXXX-inccoming (https://pkgs.dev.azure.com/XxxXxxxXxxx/XxxXxx/_packaging/PXXXXX-incoming/maven/v1): Transfer failed for https://pkgs.dev.azure.com/XxxXxxxXxxx/XxxXxx/_packaging/PXXXXX-incoming/maven/v1/pl/group/id/artifact/0.0.2/artifact-0.0.2.jar 401 Unauthorized ProxyInfo{host='proxy-host', userName='null', port=8080, type='http', nonProxyHosts='null'}
The provided user has full access for packaging in Azure.
Both proxy and server settings are actually in my setting.xml file.
What am I doing wrong?
Azure Artifacts deploy packages
When we receiving a 401 it is because maven is sending the wrong login credentials, or no credentials at all.
To resolve this issue, please add the following configuration to the <servers>...</servers> configuration node in the maven setting configuration file:
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
Note:
The id configured in setting.xml must be the same as the id
configured in pom.xml.
If you try to publish something to a releases repository and that
version already exists in the repository
If it not work for you, please check below thread for some more details:
Why am I getting a "401 Unauthorized" error in Maven?

How to specify credentials for custom maven private repo

I have used this article (https://gist.github.com/fernandezpablo85/03cf8b0cd2e7d8527063) for creating a custom maven repo, and it works. But now I have a problem with a private repo. How can I specify credentials for the private repo?
You can set in .m2/settings.xml file
Like This:
<settings>
<servers>
<server>
<id>private-repo</id>
<username>xyz</username>
<password>${pass}</password>
</server>
</servers>
</settings>
And in pom.xml:
<project>
...
<repositories>
<repository>
<id>private-repo</id>
<url>${private-repo.url}</url>
</repository>
</repositories>
...
</project>
A settings.xml file is usually found in a couple of places:
Global settings in Mavens home directory: ${maven.home}/conf/settings.xml
User settings in the user’s home: ${user.home}/.m2/settings.xml
If both files exist, their contents are merged. Configurations from the user settings take precedence.
If your settings doesn't apply you can try to override the default settings.xml file location by adding --settings flag to your mvn ... command.
sources:
https://www.baeldung.com/maven-settings-xml#settings-level
https://www.baeldung.com/maven-settings-xml#3-override-the-default-location

deploying an artifact whit pom into a given repo in command line? (without pom edition and file-deploy)

I have auto-generated java code I want to deploy in our maven repo. The generated code has a pom.xml without the repo info. I what to know if I can deploy the complete maven project in my repo without touching the pom. And by complete maven project, I mean not only the final jar using the file-deploy maven command.
The repo has security enabled and requires usr/pass
I found a way. You need to add the server to the settings file as following:
<servers>
<server>
<id>my-server</id>
<username>user</username>
<password>pass</password>
</server>
</servers>
then I deploy using the following maven command (replacing <url> for the target repo):
mvn deploy -D"altDeploymentRepository=my-server::default::<url>"

How to read properties file from one project into another project?

I have two java projects A and B, If I have a credentials.properties file in A and I want to access the same properties file in project B, Is there a way I can achieve this?
The two projects are maven build.
Try this:
<resource>
<directory>${other projects dir}/src/main/resources</directory>
<includes>
<include>*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
Give the path as a full path
Keep the property file in the class path of B and set Project B as a dependency to project A.
Commiting cleartext passwords into your source control is normally a bad idea...
How about using a shared Maven profile in your settings file? ($HOME/.m2/settings.xml):
<settings>
..
<profiles>
<profile>
<id>credentials</dev>
<activeByDefault>true</activeByDefault>
<properties>
<password1>XXXXX</password1>
<password2>YYYYY</password2>
..
..
</properties>
</profile>
..
</profiles>
..
</settings>
This approach is more Maven friendly and encryption is supported.
If you use Jenkins to build your code, you can use a plugin to manage the settings file centrally:
How to manage maven settings.xml on a shared jenkins server?
Your project can still have a default value, the key point is that the real passwords are set externally to files under source control.

Maven: How to deploy with deploy-file and custom wagon

I'm trying to use a custom maven wagon extension to deploy a jar to my own repository. Can I somehow configure in settings.xml that it recognizes the custom url scheme to be used with the specific wagon or do I have to always modify pom files to contain the wagon extension?
There doesn't need to be a base pom or any pom available when using the deploy-file. Settings.xml is the only place which is guaranteed to be there, but I can't figure out how to use it to define extensions.
OK, ok, a correction: you cannot define the <build> element inside a <profile> defined in settings.xml. You could activate the profile in settings.xml, but define it in your base-pom.
Sorry, the only other way I could think of (probably what are you looking for), is to copy the extension jar directly under $M2_HOME/lib. All $M2_HOME/lib/*.jar are put in the classpath, so this must virtually have the same effect as an <extension>.
The extension however is better, because you can more easily control which version of the extension is used (e.g. trough the base-pom).
OK just try copying the extension jar under
$M2_HOME/lib
I don't know if the comment above by Brian Fox is still valid in 2013. But in the end I had to create a minimal pom.xml in the directory where I tried to upload the artifact to enable the wagon build extension.
I had to add groupId, artifactId and version to the pom.xml so that Maven would not complain although I provided them to the deploy-file goal on the commandline (I guess deploy-file would only care about the commandline parameters though):
<project 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>your-groupId</groupId>
<artifactId>your-artifactId</artifactId>
<version>your-version</version>
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-ssh</artifactId>
<version>2.4</version>
</extension>
</extensions>
</build>
</project>
With this simple "pom.xml" in place I could execute the deploy-file finally using scp as the protocol:
mvn deploy:deploy-file -Durl=scp://shell.sourceforge.net:/home/project-web/... -DrepositoryId=repoId -Dfile=my-file.jar -DgroupId=your-groupId -DartifactId=your-artifactId -Dversion=your-version -Dpackaging=jar
You need to add the wagon extension to your top level pom.xml. Most environments have a corporate one at the top of all their projects (best practice), so this generally isn't too painful for individual developers -- they just inherit from the corporate pom.
<build>
<extensions>
<extension>
<groupId>org.apache.maven.wagon</groupId>
<artifactId>wagon-scm</artifactId>
<version>1.0-alpha-7-SNAPSHOT</version>
</extension>
<extension>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-manager-plexus</artifactId>
<version>1.0-beta-3-SNAPSHOT</version>
</extension>
<extension>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-svnexe</artifactId>
<version>1.0-beta-3-SNAPSHOT</version>
</extension>
</extensions>
</build>
<distributionManagement>
<site>
<id>my.svn.server</id>
<url>scm:svn:https://username#svn.apache.org/svn/root/module</url>
</site>
</distributionManagement>
When you register your provider, it also registers the protocol pattern as well I believe. You can see a full list of the existing providers here.
I believe it is the getScmType() method that registers the extension, but I'm not 100% certain.
/** {#inheritDoc} */
public String getScmType()
{
return "git";
}
The link to the Git provider's source can be found here.
siddhadev is right, but there are few additional things...
(I'd put this in a comment but I don't have enough reputation)
You can keep your JARs cleanly separated by putting them under:
$M2_HOME/lib/ext
You need all of the dependencies, so do something like:
cd ~/.m2/repository/org/apache/maven/wagon/wagon-ssh-external/2.2
cp wagon-ssh-external-2.2.jar $M2_HOME/lib/ext
cp wagon-ssh-external-2.2.pom pom.xml
mvn dependency:copy-dependencies -DoutputDirectory=$M2_HOME/lib/ext

Categories